Extends ActionController::TestCase::Behavior to work with RSpec.
describe WidgetsController do
describe "GET index" do
it "assigns all widgets to @widgets" do
widget = stub_model(Widget)
Widget.stub(:all) { widget }
get :index
assigns(:widgets).should eq([widget])
end
end
end
describe WidgetsController do
describe "GET index" do
it "assigns all widgets to @widgets" do
widget = Factory(:widget)
get :index
assigns(:widgets).should eq([widget])
end
end
end
describe WidgetsController do
describe "GET index" do
fixtures :widgets
it "assigns all widgets to @widgets" do
get :index
assigns(:widgets).should eq(Widget.all)
end
end
end
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)
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 views. This means that a view template need not even exist in order to run a controller spec, and you can still specify which template the controller should render.
If you prefer a more integrated approach, similar to that of Rails' functional tests, you can tell controller groups to render views with the render_views declaration:
describe WidgetsController do render_views ...
Generated with the Darkfish Rdoc Generator 2.