Mocha multiple_yields with arrays

R

Raymond O'Connor

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.multiple_yields([1,2], [3,4], [5,6])

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

James Mead

2009/11/26 Raymond O'Connor said:
I believe there's a bug in mocha that causes an unnecessary and annoying
warning message. =A0If you call multiple_yields with arrays as the arg yo= u
want yielded it will complain.

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

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

That code snippet looks wrong. The multiple_yields method is a method
on an expectation, so you must call it on the result of e.g.
object.stubs or object.expects. I suspect this was a typo and assume
you meant something more like this :-

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

In this case, Mocha will invoke the block supplied to the wibble
method 3 times and passing in 2 parameters on each invocation.

If you supply a block which defines only one parameter you will see
the warning you described :-

parameters =3D []
object.wibble { |parameter| parameters << parameter }
# =3D> warning: multiple values for a block parameter (2 for 1)
p parameters # =3D> [[1,2], [3,4], [5,6]]

If you supply a block which defines two parameters you will not see
the warning :-

parameters =3D []
object.wibble { |parameter_1, parameter_2| parameters <<
[parameter_1, parameter_2] }
p parameters # =3D> [[1,2], [3,4], [5,6]]

So the warning you are seeing is not a problem with Mocha, but due to
normal Ruby behaviour which you can see in irb :-

irb> def wibble
irb> yield(1,2)
irb> end
=3D> nil
irb> wibble { |x| p x }
(irb):4: warning: multiple values for a block parameter (2 for 1)
from (irb):2
[1, 2]
=3D> nil
irb> wibble { |x,y| p [x,y] }
[1, 2]
=3D> nil
irb> wibble { |(x,y)| p [x,y] }
[1, 2]
=3D> nil

I hope this helps. If you have any further questions about this,
please post to the Mocha mailing list [1] which would be a more
appropriate forum.

Cheers, James.

[1] http://groups.google.com/group/mocha-developer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top