Setting Up RSpec and Cucumber

Including RSpec in your code:

Add the following in your Gemfile File: Gemfile

1
2
3
group :test, :development do
  gem "rspec-rails", "~> 2.0"
end
1
bundle

Installing RSpec

1
rails g rspec:install

Configuring RSpec

File: config/application.rb

1
2
3
4
config.generators do |g|
  g.test_framework :rspec
  g.integration_tool :rspec
end

Now adding a new scaffold or resource will automatically include the RSpecs to help test the code

1
2
3
rails g scaffold book title author
rake db:migrate
RAILS_ENV=test rake db:migrate

Running the Specs

1
rspec spec

You can also specify the file name and the line number for running individual specs

Including Cucumber in your Code

Add the following in your Gemfile File: Gemfile

1
2
3
group :test, :development do
  gem 'cucumber-rails', require: false
end

Installing Cucumber

1
rails g cucumber:install

For learners who are looking to setup the cucumber for the first time

Use Traing Wheels

File: Gemfile

1
gem "cucumber-rails-training-wheels", :group => :test

Installing Cucumber rails generate cucumber_rails_training_wheels:install

Set up Cucumber Training Wheels before Scaffolding

1
2
rails generate cucumber_rails_training_wheels:feature post title:string body:text number:integer published:boolean
rails generate scaffold post title:string body:text number:integer published:boolean

Testing the Features

1
cucumber features

If you face Issues with Tableish in your features

Replace:

1
tableish('table tr', 'td,th')

With:

1
find('table').all('tr').map { |row| row.all('th, td').map { |cell| cell.text.strip } }

Any questions on this, please feel free to ask. We’re here to help…

Comments

Comments