How To Use RSpec with Rails 3
Because Rails 3 is still pretty new (less than a week old as I write this), there’s relatively little documentation about how to use RSpec with this latest version of Rails [update: what I should have said is that docs for Rspec and Rails 3 are good, but perhaps less well-organised than for older versions of Rails. See comments below.].What I want to do here is bring together the instructions I’ve found on using RSpec with Rails 3, and share some tips on how to get your BDD cycle running smoothly.
[Note: from this point on, I'll use "Rails" to mean "Rails 3"]
Installing Rspec for Rails 3
The first new thing to be aware of is the rspec-rails gem; because Rails 3 is much more modular and extensible than previous versions of the framework, this one gem can do all the work of plugging RSpec into your Rails apps.
Install the gem thus:
gem install rspec-rails --pre
The --pre tells Rubygems to install the beta version of the gem, which you’ll need for Rails 3 development.
The next step is to tell your Rails app to use the rspec-rails gem; this is done quite simply in your app’s Bundle file. Because you don’t need RSpec in production, add the gem to the development and test groups only:
# in path/to/your/app/Bundle
group :development, :test do
# the version number may be different for you.
# Use gem list rspec-rails --local on your command line
# to get the exact version number.
gem 'rspec-rails', '2.0.0.beta.20'
end
The final step is to run the following in the root of your Rails app:
script/rails generate rspec:install
This sets up a spec/ folder and some helper files.
Using RSpec
Now you’ve installed the rspec-rails gem, using RSpec is actually really simple. The gem tells Rails to use rspec to generate test files, which means that generators will create spec files without you having to pass in any extra options (if you check the options for rails generate model, you’ll see that there’s a --test-framework option. You don’t need to use this to specify RSpec; after installing rspec-rails, RSpec will be used by default).
So, to generate a Person model with corresponding person_spec.rb, all you need to do is this (the same applies to controllers, helpers, etc):
rails generate model Person
Check out spec/models/person_spec.rb to see the stub spec for your Person model. That’s really all you need to know to start using RSpec on Rails 3, but do checkout the rspec-rails repository on Github: the README gives more in-depth explanations of a lot of the points I’ve made here, and also has extra tips on writing specs for controllers, views, responses, and routes.
Pro Tips
A few cools things I’ve found using RSpec in my new Rails apps:
Helper Specs
Helpers specs have access to a special object in the helper object, which includes your helper. For example, in person_helper_spec.rb the helper object has access to all the methods in PersonHelper. This makes it really easy to test your helper methods:
# Assumes a PersonHelper with a #speak method.
describe PersonHelper do
describe '#speak' do
# roughly equivalent to:
#
# helper = Object.new
# helper.send :include, PersonHelper
# helper.speak.should == 'Hello'
it 'says Hello' do
helper.speak.should == 'Hello'
end
end
end
Spec Support Files
Although it’s not there by default, if you create a folder spec/support, then every file under that folder will be required when you run specs. This is really useful if you want to keep custom RSpec matchers, mocks, or other supporting code, in separate files (for example, you might want to keep custom matcher code in spec/support/my_matcher.rb and mocks in spec/support/mocks.rb).
Hopefully this post has helped you can a handle on how to use RSpec with the latest version of Rails. If you spot any other “pro tips”, post them in the comments! Thanks for reading.