Here are some examples of the basic form.
controller.stub(:action_name).and_raise([some error]) controller.stub(:action_name).and_return([some value])
In your particular case, I believe that the correct form is:
controller.stub(:current_user).and_return([your user object/id])
Here is a complete working example of a project I'm working on:
describe PortalsController do it "if an ActionController::InvalidAuthenticityToken is raised the user should be redirected to login" do controller.stub(:index).and_raise(ActionController::InvalidAuthenticityToken) get :index flash[:notice].should eql("Your session has expired.") response.should redirect_to(portals_path) end end
To explain my complete example, this basically means that when an ActionController::InvalidAuthenticityToken
error ActionController::InvalidAuthenticityToken
, a flash message appears anywhere in the application and the user is redirected to the action portals_controller#index
. You can use these forms to display and return specific values, check an instance of a given error, etc. There are several .stub(:action_name).and_[do_something_interesting]()
methods available.
Refresh (after adding the code): for my comment, change your code so that it reads:
require 'spec_helper' describe "Login" do before(:each) do @mock_controller = mock("ApplicationController") @mock_controller.stub(:current_user).and_return(User.first) end it "logs in" do visit '/' page.should have_content("Hey there user!") end end
jefflunt Sep 16 '11 at 19:00 2011-09-16 19:00
source share