# Mocha multiple\_yields with arrays

**URL:** <https://www.ruby-forum.com/t/mocha-multiple-yields-with-arrays/179481>\
**Category:** Ruby\
**Created:** [November 26, 2009, 2:16am UTC](https://www.ruby-forum.com/t/mocha-multiple-yields-with-arrays/179481 "2009-11-26T02:16:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [November 26, 2009, 2:16am UTC](https://www.ruby-forum.com/t/mocha-multiple-yields-with-arrays/179481/1 "2009-11-26T02:16:40Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [November 26, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/mocha-multiple-yields-with-arrays/179481/2 "2009-11-26T13:36:39Z")

</div>

2009/11/26 Raymond O’Connor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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”

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 = []  
object.wibble { |parameter| parameters \<\< parameter }

# =\> warning: multiple values for a block parameter (2 for 1)

p parameters # =\> [[1,2], [3,4], [5,6]]

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

parameters = []  
object.wibble { |parameter\_1, parameter\_2| parameters \<\<  
[parameter\_1, parameter\_2] }  
p parameters # =\> [[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  
=\> nil  
irb\> wibble { |x| p x }  
(irb):4: warning: multiple values for a block parameter (2 for 1)  
from (irb):2  
[1, 2]  
=\> nil  
irb\> wibble { |x,y| p [x,y] }  
[1, 2]  
=\> nil  
irb\> wibble { |(x,y)| p [x,y] }  
[1, 2]  
=\> 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](http://groups.google.com/group/mocha-developer)
