[mocha-developer] Beyond multiple return values

Dan North dan at tastapod.com
Sat Mar 10 07:45:04 EST 2007

Ah, that's "surprising". I haven't tried using lambdas with mocha, but 
that's not what I would expect. I expect returns to, um, return stuff. I 
would expect to use raises and executes (or maybe invokes) for 
exceptions and lambdas respectively.

returns(&block) actually executing the block seems counter-intuitive to me.

sheep.expects(:eat).with(grass).*executes*(proc {puts "chewing"})

proc_factory.expects(:create).*returns*(proc { puts "pretend the factory 
created this" })

Come to think of it, is there any way in mocha of doing the second one?


James Moore wrote:
> On 3/9/07, Dan North <dan at tastapod.com> wrote:
>> I have to agree with James - I'm not a fan of returns changing its
>> behaviour based on the return type. What if I'm writing an exceptions
>> factory, or some sort of exception processor (say something to log parts
>> of a stack trace)?
>> I'm happy with .returns(quietly) and .raises(mayhem).
> It does already, though.  For example, there's not a clean way to mock
> something that returns closures, which is probably more common than
> something that returns exceptions.  If returns() is given something that
> happens to be a lambda, it doesn't return the lambda (which is what you'd
> normally expect), it calls it and returns its value instead.  I'm just
> suggesting adding to that concept:
> case return_value
> when its a Proc: call the proc, return the result # Existing behavior
> when its an Exception, or an instance of an exception, raise it # Additional
> behavior
> else return return_value
> end
> I do see your point though - I'm not convinced this is worth arguing
> strongly for, but I do think it fits well with the current code.
>  - James Moore
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

More information about the mocha-developer mailing list