Rails 3, RSpec and Cucumber setup

This quick how-to runs through creating a new Rails 3 application with both RSpec and Cucumber for testing (rather than Test::Unit).

Rails 3 requires (the somewhat confusingly versioned) RSpec 2 and rspec-rails. Similarly, Cucumber uses cucumber-rails for bootstrapping your Rails app and provides generators for creating features.

Install RSpec 2

sudo gem install rspec-rails --pre

Install Cucumber

sudo gem install cucumber-rails

Create a new Rails 3 application

Ensure you have the latest version of Rails 3 installed then create a new application, skipping creation of the test directory since we’ll be using RSpec and Cucumber (with spec and features) instead.

rails new  --skip-testunit

Add rspec-rails to the :test and :development groups to the Gemfile.

group :test, :development do
    gem 'rspec-rails', '>= 2.0.0.beta.22'
end

Add cucumber-rails and dependencies to the :cucumber group in the Gemfile:

group :cucumber do
    gem 'capybara'
    gem 'database_cleaner'
    gem 'cucumber-rails'
    gem 'cucumber'
    gem 'spork'
    gem 'launchy'
end

Ensure all gems are installed by using Bundler:

sudo bundle install

Then install the RSpec extensions (creates the spec directory and creates a rake task):

rails generate rspec:install

Now we can specify that Rails itself uses RSpec when running generators by adding the following inside config/applicaton.rb:

config.generators do |g|
  g.test_framework :rspec
end

Next, bootstrap your Rails app, for Cucumber:

rails generate cucumber:install --rspec --capybara

That’s it! Now the following rake tasks exist to execute your specs or cucumber features (by default rake will execute both).

rake spec
rake cucumber

About this entry