Testing Rails 3.2.3 Application Using Cucumber (BDD)

What is Cucumber?

Cucumber is used for Behavior Driven Development. Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid – all rolled into one format.

Cucumber works with Ruby on Rails, Java, .NET, Flex or web applications written in any language. It has been translated to over 40 spoken languages. Cucumber also supports more succinct tests in tables – similar to what FIT does.

Install cucumber for Rails 3:

Install Cucumber, Rspec-rails and capybara gem

sudo gem install cucumber-rails
sudo gem install database_cleaner
sudo gem install rspec-rails
sudo gem install capybara
  • Cucumber is a behavior driven development (BDD) framework in particular good for integration and functional tests

  • RSpec is a behavior driven development (BDD) framework for low-level testing in the Ruby language
  • database_cleaner performs DB cleanup in testing
  • capybara simulating a browser, automating a browser or setting expectations using the matchers.

 

Using Cucumber to test rails 3 application:

1. Create a new Rails application store

$rails new store -d mysql

2. Edit the Gemfile

Include the Cucumber gems in a Rails 3 application

   group :test, :development do
        gem 'rspec-rails'
        gem 'cucumber-rails'
        gem 'capybara'
        gem 'database_cleaner'
 end
3. bundle install

4. Install the cucumber skeleton files to a Rails 3 application.

$ rails generate cucumber:install 

The cucumber-rails generator creates the directories:

features/step_definitions

features/support

The cucumber-rails generator creates the files:

config/cucumber.yml

features/support/env.rb

lib/tasks/cucumber.rake

script/cucumber

5. rake db:create
6. rake db:migrate

7. Run the Cucumber features

$rake cucumber

Create cucumber features:
  • Create a Cucumber Feature for RoR 3 application testing

$vi features/manage_store.feature

  • Cucumber Feature is the business user testing specification written in plain English with a specific format

Feature Format

 Feature: ...
   In order ...
     Some Actor ...
     Should ...

    Scenarior: ...
       Given ...
       And ...

     When ...
     AND ...

     Then ...
     AND ...

Implement the testing steps and Ruby on Rails model code for Cucumber on Rails :

  • Create Cucumber step definitions

$vi features/step_definitions/order_steps.rb

  • Cucumber uses regular expression to match string and pass it to the step definitions
  • Run Cucumber
$rake cucumber
An example might be the ability to manage companies:
Feature: Manage companies

 In order to keep track of companies user should be able to manage companies

Scenario: Create a new company

 Given I am logged in

 When I create a new company named Acme

 Then I should see that a company named Acme exists 

features/

companies.feature

steps/

company_steps.rb

Given == Setup

Given "I am logged in" do

user = Factory(:user)

visits new_session_path

fills_in ‘Login’,

:with => user.login

fills_in ‘Password’, :with => user.password

clicks_button ‘Login’

end

Given "I am logged in" do

user = Factory(:user)

visits new_session_path

fills_in 'Login',

:with => user.login

fills_in 'Password', :with => user.password

clicks_button 'Login'

end

When == Change

When "I create a new company named $name" do |name|

visits new_company_path

fills_in 'Name', :with => name

clicks_button 'Create'

end

Then == Outcome

Then "I should see that a company named $name exists" do |name|

response.body.should =~ Regexp.new(name)

end

At every step you need to run cucumber and check for the result.

Get in touch with us.

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish