Controllers

Controller Examples live in $RAILS_ROOT/spec/controllers/.

In contrast to Test::Unit, where the controller is initialized in the setup method,
with Spec::Rails you don’t. Instead, pass the controller class to the describe method.
Spec::Rails automatically instantiates a controller for you, which you can access
from the examples (it blocks) using the controller method.

Isolation from views

By default, Spec::Rails will run your controller actions in isolation from
their related views. This allows you to spec your controllers before the views
even exist, and will keep the specs from failing when there are errors in your
views.

Optional integration with views

If you prefer to integrate views (a la rails functional testing) you can by including
the keyword/commmand “integrate_views”.

describe ArticlesController do
  integrate_views
  ...
end

When you integrate views in the controller specs, you can use any of the
expectations that are specific to views as well. Read about View Examples
to learn more.

Isolation from the database

We strongly recommend that you use RSpec’s mocking/stubbing framework
to intercept class level calls like :find, :create and
even :new to introduce mock instances instead of real active_record instances.

This allows you to focus your specs on the things that the controller does and not
worry about complex validations and relationships that should be described in
detail in the Model Examples

account = mock_model(Account)
Account.should_receive(:find).with("37").and_return(account)

or

account = mock_model(Account)
Account.stub!(:find).and_return(account)

See Mocks and Stubs for more information about the
built in mocking/stubbing framework.

Response Expectations

These expectations will work whether in isolation or integration mode. See Spec::Rails::Expectations for details.

response.should be_success

Passes if a status of 200 was returned. NOTE that in isolation mode, this will
always return true, so it’s not that useful – but at least your specs won’t break.

response.should be_success

response.should be_redirect

Passes if a status of 300-399 was returned.

response.should be_redirect

response.should render_template

get 'some_action'
response.should render_template("path/to/template/for/action")

response.should have_text

get 'some_action'
response.should have_text("expected text")

response.should redirect_to

get 'some_action'
response.should redirect_to(:action => 'other_action')

The following forms are supported:

response.should redirect_to(:action => 'other_action')
response.should redirect_to('path/to/local/redirect')
response.should redirect_to('http://test.host/some_controller/some_action')
response.should redirect_to('http://some.other.domain.com')

assigns, flash and session

Use these to access assigns, flash and session.

assigns[:key]
flash[:key]
session[:key]

Routing Expectations

Specify the paths generated by custom routes.

route_for(:controller => "hello", :action => "world").should == "/hello/world"

Specify the parameters generated from routes.

params_from(:get, "/hello/world").should == {:controller => "hello", :action => "world"}