[mocha-developer] Beyond multiple return values

James Mead jamesmead44 at gmail.com
Mon Mar 12 06:43:59 EDT 2007

On 10/03/07, Dan North <dan at tastapod.com> wrote:
> 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?
> Cheers,
> Dan
> 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

James & Dan - you both make very valid points. Thanks.

I'd like to make the API less surprising. Surprising is BAD!

Specifically I'd like to make it possible to return exactly what is
specified in the 'returns' method, even if that is a Proc instance.

My colleague Chris pointed out that there is a similar issue in the 'with'
method which can accept a block. I'd like to fix that too.

My colleagues and I have been chucking a few ideas around. I'll try and post
about these ideas as soon as I have some time.

One of the issues is providing a simple migration path for those with lots
of tests relying on the current behaviour.

I'd be interested whether you have any thoughts.


More information about the mocha-developer mailing list