When to send "post" and "get" in blocks

Hi,
I’m new to RSpec. I have a question about how to write controller specs.

Here’s a code generated by scaffolding (with Rspec).

def do_get
  get :index
end

it "should be successful" do
  do_get
  response.should be_success
end

it "should render index template" do
  do_get
  response.should render_template('index')
end

it "should find all books" do
  Book.should_receive(:find).with(:all).and_return([@book])
  do_get
end

it "should assign the found books for the view" do
  do_get
  assigns[:books].should == [@book]
end

end

###################

What I don’t understand is that, sometimes, “get” is done before an
expectation(i.e., a line that contains “should”), but other times, the
order
is the opposite.

In the third spec above, the sentence that has “should” comes first and
do_get later. In my view, “get :index” needs to be done first to trigger
“index” action. And the “index” action should trigger
“Book.should_receive(:find)…”. I’m a bit confused about all of this.

Mike

Hi Mike,

You should place the spectation before the “do_method” (a call to a
controller method) when you are stating that something must happen
inside your controller code, like asserting that some object should
receive a specific method call, like your example, where it’s stating
that the Book class should receive the message :find (or should
receive a call to the find method) with :all as a parameter, you place
this before the “do_method” 'cos the call will happen inside the
“do_method”, so you have to explain in advance what you spect to see.

And a spectation is placed after a “do_method” when you want to assert
some state after the request is processed, like asserting that the
correct template was rendered, asserting that a non-logged in user
should receive a redirect to the login page or asserting that a
variable will be available for your views. You are doing this after
the “do_method” 'cos only after the controller execution you will have
access to the generated response.

On Sat, Jun 7, 2008 at 12:46 AM, Mike S. [email protected]
wrote:

end
it "should assign the found books for the view" do

“index” action. And the “index” action should trigger
“Book.should_receive(:find)…”. I’m a bit confused about all of this.
Mike


Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208

Mauricio,
Thanks so much for your very clear explanation!! You saved my life.

Thanks.

Mike.