June 5, 2010
Rails 3, RSpec, and Cucumber
To install the latest rails pre-release (Rails 3 beta 3 at the time of this writing):
gem install rails --pre
It’s got a lot of dependencies nowadays, so enjoy watching them all scroll past.
Now let’s create a little Rails app to try it out. The laws of Rails physics dictate that it must be a blog, so:
rails new blog --skip-testunit
(If you’re on a version of Rails 3 earlier than beta 4, you’ll need to leave out the “new” from the above command.)
Given we’re going to be installing RSpec, I've told it to skip over creating the
If you’re a git user (which you should be), now is a good time to check stuff in:
cd blog git init git add . git commit -m "Initial import."
Rails 3 has, rather thoughfully, already set up a
.gitignore file for you, and sprinkled
.gitkeep files in directories that need to stick around.
Ok, now you need to start getting friendly with Bundler. Edit your app’s
Gemfile and add the following:
group :test do gem 'rspec-rails', '>= 2.0.0.beta.10' end
Then, to install the gems, just run this from your app’s root directory:
Now to add the necessary bits and bobs to your app:
script/rails generate rspec:install
(Notice how all the various commands under the script directory have gone away in Rails 3? Everything now lives under the single
Magic! Now you can tell Rails 3’s fancy new generators to always use RSpec rather than Test::Unit. Open up your
config/applicaton.rb and find the commented out section relating to generators. Add this:
config.generators do |g| g.test_framework :rspec end
Let’s try it out:
$ script/rails generate model post title:string body:text published:boolean invoke active_record create db/migrate/20100605021005_create_posts.rb create app/models/post.rb invoke rspec create spec/models/post_spec.rb
Check it out! A migration, a model, and a spec file. Fancy, huh? Does it really work?
$ rake db:migrate == CreatePosts: migrating ==================================================== -- create_table(:posts) -> 0.0021s == CreatePosts: migrated (0.0022s) =========================================== $ rake spec * Finished in 0.01027 seconds 1 example, 0 failures, 1 pending Pending: Post add some examples to (or delete) ./spec/models/post_spec.rb (Not Yet Implemented) # ./spec/models/post_spec.rb:4
Yep, sure does! It even gives you a helpful pending spec to cajole you into writing some proper tests.
Setting up Cucumber is a similar process. First add this to your
group :cucumber do gem 'capybara' gem 'database_cleaner' gem 'cucumber-rails' gem 'cucumber', '0.7.3' gem 'rspec-rails', '>= 2.0.0.beta.10' gem 'spork' gem 'launchy' # So you can do Then show me the page end
Then use Bundler to install everything:
And set up your Rails app:
script/rails generate cucumber:skeleton --rspec --capybara
Now you’re off and running, and can start adding features and step definitions to your heart’s content.
I recommend not using the feature generator. It generates feature-coupled steps, which is considered an anti-pattern. Write your own feature files from scratch, and put your steps in files corresponding to the models they work with. The RSpec Beta Book has really good advice on how to structure your features and steps.
(As of this writing,
rake cucumber spits out a rather noisy warning when you run it. If you’re lucky, they've already been fixed by the time you’re reading this. If not, you can safely ignore them.)
I strongly recommend you check out the latest RailsCasts on Rails 3, in which Ryan Bates is doing an awesome job of covering the best new features.