BDD in Rails

Step 1 :First install the following gems

  • gem install cucumber
  • gem install rspec
  • gem install webrat
  • gem install factory-girl
  • gem install cucumber-rails
  • gem install database_cleaner

Step 2 :in environments/test.rb

config.gem ‘rspec-rails’, :lib=>false
config.gem ‘rspec’, :lib=>false
config.gem ‘cucumber’
config.gem ‘webrat’

Step 3 : in features/support/env.rb

require ‘cucumber/rails/rspec’
require ‘webrat/integrations/rspec-rails’

Step 4 : in command prompt

rake gems:install RAILS_ENV=test
script/generate cucumber
script/generate rspec

Step 5 : in /features/signin.feature ( create a new file)

Feature:
So that I can manage my own personal information securely
As a registered user
I want to log into the system

Scenario:

Given I am an existing user
When I fill in “login” with “selva”
And I fill in “password” with “selvaonrails”
And I press “Log in”
And I should see “Logged in successfully”
Then I am on the home page

Scenario:

Given I am a nonexisting user
When I fill in “login” with “baduser”
And I fill in “password” with “badpassword”
And I press “Log in”
Then I should see “Username / Password incorrect”

Step 6 : In /features/step_definitions/user.rb (create a new file)

Given /^a valid user$/ do
visit “/sessions/new”
fill_in(“login”, :value=>”selva”)
fill_in(“password”, :value=>”selvaonrails”)
end

Given /^I am a nonexisting user$/ do
visit “/sessions/new”
end

Step 7 : in /features/support/paths.rb

when /the home\s?page/
‘/’

Step 8 : run in command prompt
cucumber features/signin.feature