Order of the get call and xxx.should

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

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

**Note: These are specs from different describe blocks run under
different authorize conditions.

Here is the do_get

def do_get
get :index, {:account_id => 1}
end

Notice the difference to where the do_get method is called. I had an
error and it was due to the order not being as is shown above. In the
docs it shows the actual request to be made before the assertion
statement, but that doesn’t work in the second example.

Thanks

On Jan 30, 2008 12:54 PM, Chris O. [email protected] 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

Because the response object doesn’t exist before the ‘get’.

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

Because we need to stub the method on the controller before the request
is run.
i.e., prevent it from running.

The former is checking that something happened, which didn’t exist
before the
call was run. Whereas the latter is stubbing the method as well as
raising if it doesn’t.

Courtenay

Chris O. wrote:

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:
http://blog.davidchelimsky.net/articles/2007/11/06/before_action-after_action

-Ben

Because the response object doesn’t exist before the ‘get’.

That makes things clearer. There have been a couple times that I have
put the response.should call before the do_get based on the reason you
mentioned, in setting up a stub before the call to the method is made.
Then after getting an error for that I saw in the documentation that it
was to be placed after, which is what caused some confusion.

Thanks for the help, it is very much appreciated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs