[mocha-developer] Stubbing yielding methods

John Pywtorak jpywtora at calpoly.edu
Fri Sep 21 19:18:23 EDT 2007

I would weigh in that something like yields is needed and possibly the 
suggested patches as well.  I do agree that it seems to work the wrong 
way.  I went and looked at how I had used yields and found what I think 
validates my assertion above.

Take for example: IO.open, IO.popen, etc.

puts IO.popen("ls", "r") { |pipe| pipe.read }

OK, so this example is contrived; However, I do think it points out some 
important things.  It is desirable to have expectations of both the call 
to popen, or the block, or both; However, rather than yield as a 
modifier to the methods expectations, separate the block's expectations, 
and return values out.  Basically rather than one statement combining 
the two, require two statements.

James Mead wrote:
> I've just been tying my brain in knots looking at bug #8687 (
> http://rubyforge.org/tracker/index.php?func=detail&aid=8687&group_id=1917&atid=7477
> ).
> I've been (1) trying to work out whether there is anything logically wrong
> with Mocha's existing behaviour and (2) whether Mocha should support the
> requested functionality.
> It all centres around the use of the Expectation#yields method. I've put a
> couple of examples together to try and help me think about it. The first one
> (http://pastie.caboo.se/99412) is a simplified version of the example in the
> bug report using a block and stubbing a method that yields to that block.
> In the second example (http://pastie.caboo.se/99413) I tried to think about
> how it would work if I converted the block to a method on an object. I
> slightly ran out of steam on this one when I realised that in this case when
> I realised Executor#execute wouldn't be called because the implementation of
> Collaborator#yielding_method is stubbed.
> This made me wonder if the Expectation#yields method should exist at all! In
> particular, in the block example, it feels odd because you are going from
> real code (ClassUnderTest) into mock code (mocked version of Collaborator)
> and back into real code (the block itself). I wonder if this is really
> sensible and/or useful.
> Has anybody got any thoughts?
> Thanks.

More information about the mocha-developer mailing list