Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


rspec-2 for rails-3 with lightweight extensions to each

Note: Use rspec-rails-1.3 for rails-2.


gem install rspec-rails

This installs the following gems:




group :test, :development do
  gem "rspec-rails", "~> 2.6"

It needs to be in the :development group to expose generators and rake tasks without having to type RAILS_ENV=test.

Now you can run:

rails generate rspec:install

This adds the spec directory and some skeleton files, including the "rake spec" task.


If you type script/rails generate, the only RSpec generator you'll actually see is rspec:install. That's because RSpec is registered with Rails as the test framework, so whenever you generate application components like models, controllers, etc, RSpec specs are generated instead of Test::Unit tests.

Please note that the generators are there to help you get started, but they are no substitute for writing your own examples, and they are only guaranteed to work out of the box for the default scenario (ActiveRecord & Webrat).

Webrat and Capybara

You can choose between webrat or capybara for simulating a browser, automating a browser, or setting expectations using the matchers they supply. Just add your preference to the Gemfile:

gem "webrat"
gem "capybara"

Living on edge

Bundler makes it a snap to use the latest code for any gem your app depends on. For rspec-rails, you'll need to point bundler to the git repositories for rspec-rails and the other rspec related gems it depends on:

gem "rspec-rails",        :git => "git://"
gem "rspec",              :git => "git://"
gem "rspec-core",         :git => "git://"
gem "rspec-expectations", :git => "git://"
gem "rspec-mocks",        :git => "git://"

Run bundle install and you'll have whatever is in git right now. Any time you want to update to a newer head, just run bundle update.

Keep in mind that each of these codebases is under active development, which means that its entirely possible that you'll pull from these repos and they won't play nice together. If playing nice is important to you, stick to the published gems.

Backwards compatibility

This is a complete rewrite of the rspec-rails extension designed to work with rails-3.x and rspec-2.x. It will not work with older versions of either rspec or rails. Many of the APIs from rspec-rails-1 have been carried forward, however, so upgrading an app from rspec-1/rails-2, while not pain-free, should not send you to the doctor with a migraine.

Known issues


Request Specs

Request specs live in spec/requests, and mix in behavior ActionDispatch::Integration::Runner, which is the basis for Rails' integration tests. The intent is to specify one or more request/response cycles from end to end using a black box approach.

describe "home page" do
  it "diplays the user's username after successful login" do
    get "/login"
    assert_select "form.login" do


This example uses only standard Rails and RSpec API's, but many RSpec/Rails users like to use extension libraries like FactoryGirl and Capybara:

describe "home page" do
  it "diplays the user's username after successful login" do
    user = Factory(:user, :username => "jdoe", :password => "secret")
    visit "/login"
    fill_in "Username", :with => "jdoe"
    fill_in "Password", :with => "secret"
    click_buton "Log in"

    page.should have_selector(".header .username", :content => "jdoe")




Controller Specs

Controller specs live in spec/controllers, and mix in ActionController::TestCase::Behavior, which is the basis for Rails' functional tests.

with fixtures

describe WidgetsController do
  describe "GET index" do
    fixtures :widgets

    it "assigns all widgets to @widgets" do
      get :index
      assigns(:widgets).should eq(Widget.all)

with a factory

describe WidgetsController do
  describe "GET index" do
    it "assigns all widgets to @widgets" do
      widget = Factory(:widget)
      get :index
      assigns(:widgets).should eq([widget])

with stubs

describe WidgetsController do
  describe "GET index" do
    it "assigns all widgets to @widgets" do
      widget = stub_model(Widget)
      Widget.stub(:all) { [widget] }
      get :index


In addition to the stock matchers from rspec-expectations, controller specs add these matchers, which delegate to rails' assertions:

response.should render_template(*args)
# => delegates to assert_template(*args)

response.should redirect_to(destination)
# => delegates to assert_redirected_to(destination)

isolation from views

RSpec's preferred approach to spec'ing controller behaviour is to isolate the controller from its collaborators. By default, therefore, controller example groups do not render the views in your app. Due to the way Rails searches for view templates, the template still needs to exist, but it won't actually be loaded.

NOTE that this is different from rspec-rails-1 with rails-2, which did not require the presence of the file at all. Due to changes in rails-3, this was no longer feasible in rspec-rails-2.


If you prefer a more integrated approach, similar to that of Rails' functional tests, you can tell controller groups to render the views in the app with the render_views declaration:

describe WidgetsController do
  # ...

Upgrade note

render_views replaces integrate_views from rspec-rails-1.3


Use assigns(key) to express expectations about instance variables that a controller assigns to the view in the course of an action:

get :index
assigns(:widgets).should eq(expected_value)

View specs

View specs live in spec/views, and mix in ActionView::TestCase::Behavior.

describe "events/index.html.erb" do
  it "renders _event partial for each event" do

describe "events/show.html.erb" do
  it "displays the event location" do
    assign(:event, stub_model(Event,
      :location => "Chicago"
    rendered.should contain("Chicago")

View specs infer the controller name and path from the path to the view template. e.g. if the template is "events/index.html.erb" then:

controller.controller_path == "events"
controller.request.path_parameters[:controller] == "events"




assign(key, val)

Use this to assign values to instance variables in the view:

assign(:widget, stub_model(Widget))

The code above assigns stub_model(Widget) to the @widget variable in the view, and then renders the view.


@widget = stub_model(Widget)
render # @widget is available inside the view


Upgrade note

# rspec-rails-1.x
assigns[key] = value

# rspec-rails-2.x
assign(key, value)


This represents the rendered view.

rendered.should =~ /Some text expected to appear on the page/

Upgrade note

# rspec-rails-1.x
response.should xxx

# rspec-rails-2.x
rendered.should xxx

Model specs

Model specs live in spec/models.

describe Articles do
  describe ".recent" do
    it "includes articles published less than one week ago" do
      article = Article.create!(:published_at => - 1.week + 1.second)
      Article.recent.should eq([article])

    it "excludes articles published at midnight one week ago" do
      article = Article.create!(:published_at => - 1.week)
      Article.recent.should be_empty

    it "excludes articles published more than one week ago" do
      article = Article.create!(:published_at => - 1.week - 1.second)
      Article.recent.should be_empty

Routing specs

Routing specs live in spec/routing.

describe "routing to profiles" do
    { :get => "/profiles/jsmith" }.should route_to(
      :controller => "profiles",
      :action => "show",
      :username => "jsmith"

  it "does not expose a list of profiles" do
    { :get => "/profiles" }.should_not be_routable

Upgrade note


Helper specs

Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.

Provides a helper object which mixes in the helper module being spec'd, along with ApplicationHelper (if present).

describe EventsHelper do
  describe "#link_to_event" do
    it "displays the title, and formatted date" do
      # EventsHelper and all of Rails' built-in helpers
      helper.link_to_event.should =~ /Ruby Kaigi, 27 Aug, 2010/


rspec-rails exposes domain-specific matchers to each of the example group types. Most of them simply delegate to Rails' assertions.


  • Available in all specs.
  • Primarily intended for controller specs
object.should be_a_new(Widget)



  • Delegates to Rails' assert_template.
  • Available in request, controller, and view specs.

In request and controller specs, apply to the response object:

response.should render_template("new")

In view specs, apply to the view object:



  • Delegates to assert_redirect
  • Available in request and controller specs.
response.should redirect_to(widgets_path)


  • Delegates to Rails' assert_routing.
  • Available in routing and controller specs.
{ :get => "/widgets" }.should route_to(:controller => "widgets", :action => "index")


Passes if the path is recognized by Rails' routing. This is primarily intended to be used with should_not to specify routes that should not be routable.

{ :get => "/widgets/1/edit" }.should_not be_routable



Also see

Something went wrong with that request. Please try again.