[rspec-users] Order of the get call and xxx.should

Ben Mabey ben at benmabey.com
Wed Jan 30 16:02:50 EST 2008

Chris Olsen wrote:
> Just out of curiosity, why is that the following .should calls have to
> differ to work?
> The first is a normal check on the if the user is redirected if not
> logged in
> =========
> it "should redirect the user to the login screen" do
>     do_get
>     response.should redirect_to(new_session_url)
>   end

Here you are checking the return value of the do_get essentially, as teh 
get will populate the response object.  So you need to actually make the 
call to the action before you can look at its response.
> The second is checking to ensure that the proper user validation method
> is called
> =========
> it "should validate the user" do
>     controller.should_receive(:authorized?).and_return(true)
>     do_get
>   end

Here you are setting up an expectation with a mock.  You are expecting 
that that object will receive that call during the action you are about 
to call.
This is the core of mocking- being able to test how things are 
interacting with each other.

The order of the calls can be confusing at first.  You just have to keep 
in mind what you are checking.... The returned response or how the 
action interacts with other object.
For a more unified way of writing tests that are a little more explicit 
check out this blog post:


More information about the rspec-users mailing list