[rspec-users] [newbie]: How to test for operations inside a block?
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
> little Socratic on your ass*
I do appreciate the fact you're not going to give me the answer, but the
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
> do you understand what's going on? When is 'puts result' executed? What
> causes it to be executed? Where does 'result' get its value? What does
> 'yield' keyword do? If you wanted to replace authenticate() with a stub
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
> 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
> 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,
watched the Peepcode's RSpec series many times and still have this
Now, the "and_return" is about the "return value of the mock", while the
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')
registration = %w( a b c )
@controller.should_receive(:authenticate_with_open_id).and_yield(result, at identity_url,registration)
puts "Hello world"
Which doesn't work at all.
The complaints are about the row marked with:
--->>A: Mock 'Object' received unexpected message :successful? with (no
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
> *Pulp Fiction reference, in case someone reading this thinks I'm merely
> being obnoxious.
I really thank you for your patience and time.
Posted via http://www.ruby-forum.com/.
More information about the rspec-users