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

Mark Wilden mark at mwilden.com
Mon Sep 22 17:56:07 EDT 2008

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

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 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
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!


*Pulp Fiction reference, in case someone reading this thinks I'm merely
being obnoxious.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080922/6259a32d/attachment.html>

More information about the rspec-users mailing list