[rspec-users] [newbie]: How to test for operations inside a block?

Carmine Moleti lists at ruby-forum.com
Tue Sep 23 03:36:00 EDT 2008


Mark Wilden wrote:
> Carmine, someone could just give you the answer, but pardon me if I get 
> a
> little Socratic on your ass*

I do appreciate the fact you're not going to give me the answer, but the 
pointers.

And, at the risk of appearing dumber than what I am, I'll try to reply.

> Given this code (which is similar to the code you're testing):
> 
> authenticate() do |result|
>   puts result
> end
> 
> do you understand what's going on? When is 'puts result' executed? What 
> code
> causes it to be executed? Where does 'result' get its value? What does 
> the
> 'yield' keyword do? If you wanted to replace authenticate() with a stub 
> for

When authenticate gets called, it will call the associated block passing 
"result" to it,
so that the code within the block can do its operations over "result" 
(and perhaps return
something to the caller).

> testing, how would 'result' get its value? Do you understand the purpose 
> of
> and_return in RSpec mocking? What would and_yield then do?
> 
> If you're unclear on any of these points, take a step back and refresh 
> your
> memory on the subject of blocks and methods that take blocks (like
> authenticate_with_open_id()) . You'll be glad you did!

I did re-read the topic of blocks in ruby (Programming Ruby 2nd Ed., 
page 47-49) which,
taking into account my bad memory, can't do any harm.

What I don't have very clear is the whole RSpec way of working and, yes, 
I've
watched the Peepcode's RSpec series many times and still have this 
feeling.

Now, the "and_return" is about the "return value of the mock", while the 
"and_yield" is
about "passing something to the associated block". Right?

Having said that, Carmine.expects(:something).like_that(:to_work):

     @user = create_operator_user
     result = mock('Object')
#     result.should_receive(:successful?).and_return(:true)
     registration = %w( a b c )
     @controller.should_receive(:authenticate_with_open_id).and_yield(result, at identity_url,registration) 
do |a_result,an_identity,a_registration|
       puts "Hello world"
--->>A        a_result.should_receive(:successful?).and_return(:true)
--->>B        User.should_receive(:create_with_role).and_return(@user)
        response.should redirect_to(users_path)
     end
     do_verb

Which doesn't work at all.
The complaints are about the row marked with:
--->>A: Mock 'Object' received unexpected message :successful? with (no 
args)

if I remove the # on "result.should..." this error disappears, but I get 
the one at "--->>B"
which I guess is for the very same reason of the first one.

It seems to me that the code in the block never gets called, indeed I 
prooved it by placing
a "puts" statement in it whose message doesn't get printed.

Now, It would be nice if you could give me pointers to RSpec 
docs/examples suitable for dumbs
like me.

> *Pulp Fiction reference, in case someone reading this thinks I'm merely
> being obnoxious.

Great movie.

I really thank you for your patience and time.
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list