Mocha multiple_yields with arrays

275 views
Skip to first unread message

Phil

unread,
Nov 27, 2009, 2:16:10 PM11/27/09
to mocha-developer
I believe there's a bug in mocha that causes an unnecessary and
annoying warning message. If you call multiple_yields with arrays as
the arg you want yielded it will complain.

object.expects(foo).multiple_yields([1,2], [3,4], [5,6])

=> prints a warning to the effect: "multiple values for a block
parameter 2 for 1"

In other words you should be able to yield an array with multiple args
when the block expects 1 variable without getting this warning

No Array
>> def wibble
>> yield(1,2)
>> end
=> nil
>> wibble { |x| puts x }
(irb):11: warning: multiple values for a block parameter (2 for 1)
from (irb):9

Array
>> def wibble
>> yield([1,2])
>> end
=> nil
>> wibble { |x| puts x }
1
2

James Mead

unread,
Nov 27, 2009, 6:51:15 PM11/27/09
to mocha-d...@googlegroups.com
2009/11/27 Phil <phuib...@gmail.com>:
In Expectation#multiple_yields, the block supplied to the stubbed
method is invoked multiple times per invocation of the stubbed method.
This means that within a single multiple_yields statement, we need to
be able to specify a different set of parameters for each invocation
of the block. There may be any number of parameters for each of these
invocations. So the parameters for each invocation are supplied as an
array. When one of the parameters for one of the block invocations is
itself an array, it's easy to get confused.

The following line :-

object.stubs(:foo).multiple_yields([1,2], [3,4], [5,6])

Mimics the behaviour of a method like this :-

def foo
yield(1, 2)
yield(3, 4)
yield(5, 6)
end

If you want to mimic the behaviour of a method which yields a single
array parameter 3 times like this :-

def foo
yield([1, 2])
yield([3, 4])
yield([5, 6])
end

Then you need a line like this :-

object.stubs(:foo).multiple_yields([[1,2]], [[3,4]], [[5,6]])

In this case, if you supply a block with only one parameter, you
should not get the warning you describe, because each invocation
passes a single array parameter.

object.foo { |only_parameter| p only_parameter }
# => [1, 2]
# => [3, 4]
# => [5, 6]

So unless I'm missing something, I don't think there is bug in Mocha.

I hope this helps.

Cheers, James.

Phil

unread,
Dec 1, 2009, 9:05:35 PM12/1/09
to mocha-developer
Ahh I didn't realize that there was magic going on when you pass it
arrays. My understanding it worked as such:

object.stubs(:foo).multiple_yields(1,2,3)
object.foo #yields(1), yields(2), yields(3)

object.stubs(:foo).multiple_yields([1,2], [3])
object.foo #yields([1,2], [3])

Your example makes it really clear how it really works- I think even
more so than the examples in the docs. Thanks!




On Nov 27, 3:51 pm, James Mead <jamesmea...@gmail.com> wrote:
> 2009/11/27 Phil <phuibon...@gmail.com>:

James Mead

unread,
Dec 2, 2009, 5:36:13 AM12/2/09
to mocha-d...@googlegroups.com
Hi Phil,

I'm glad these examples make things clearer.

I don't think there's any "magic" going on, but I agree the
documentation could be improved and I've added a ticket [1] to this
effect.

Cheers, James.

[1] http://floehopper.lighthouseapp.com/projects/22289/tickets/59


2009/12/2 Phil <phuib...@gmail.com>:
> --
>
> You received this message because you are subscribed to the Google Groups "mocha-developer" group.
> To post to this group, send email to mocha-d...@googlegroups.com.
> To unsubscribe from this group, send email to mocha-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mocha-developer?hl=en.
>
>
>
Reply all
Reply to author
Forward
0 new messages