[mocha-developer] counter-intuitive behaveour when passing a proc to Mocha::Expectation#returns

Tomas Pospisek tpo2 at sourcepole.ch
Thu Jan 11 06:54:26 EST 2007

Let's say that I have a procedure that:

* gets the number of bytes to write into some file
* and returns the number of successfully written bytes

   def save( text, len )

This procedure is being used all over the place especially inside the method
"process" which I want to test. Thus in order not to polute my filesystem I stub
the "save" procedure. But of course in order for my "process" method I need the
"save" procedure to return the correct number of bytes. Thus naively I'd do
something like this:

   stubs( :save ). returns( lambda { eval "len" } )

Unfortunately this doesn't work, since the "stubs" method doesn't at all take
parameter names into account. As far as I can see, this is the place where the
actual method replacement is being done:

   def define_new_method
     stubbee.metaclass.class_eval "def #{method}(*args, &block)
     mocha.method_missing(:#{method}, *args, &block); end"

and it just passes all the arguments as an array. I'm not suggesting the fix for
the problem at hand is trivial - I can't see at a glance how one can determine
the signature of some method wrt to its parameter*names*, however from the
Mocha-user's aka tester's perspective Mocha's behaveour is quite confusing.

*t, exploring Mocha

This message was sent using IMP, the Internet Messaging Program.

More information about the mocha-developer mailing list