On 29/09/06, <b class="gmail_sendername">Jay</b> <<a href="mailto:jay@jayfields.com">jay@jayfields.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm not sure if someone has already addressed this issue, but if not,<br>here goes.<br><br>I need the following behavior:<br> def test_generate(documentation = '/test_documentation/',<br>destination = '/destination/')<br>
yield_results = ['/', 'filename.yaml', 'blah.xml']<br> Find.expects(:find).with(documentation).yields(lambda<br>{ yield_results.shift } )<br> ...<br> end<br><br>So I made these changes...<br><br>Index: test/mocha/expectation_test.rb
<br>===================================================================<br>--- test/mocha/expectation_test.rb (revision 62)<br>+++ test/mocha/expectation_test.rb (working copy)<br>@@ -91,6 +91,11 @@<br> assert_equal parameters_for_yield, yielded_parameters
<br> end<br>+ def test_should_yield_with_block_result<br>+ expectation = new_expectation.yields( lambda { 'lambda return<br>value' } )<br>+ expectation.invoke() { |*parameters| assert_equal ['lambda<br>return value'], parameters }
<br>+ end<br>+<br> def test_should_return_specified_value<br> expectation = new_expectation.returns(99)<br> assert_equal 99, expectation.invoke<br>Index: lib/mocha/expectation.rb<br>===================================================================
<br>--- lib/mocha/expectation.rb (revision 62)<br>+++ lib/mocha/expectation.rb (working copy)<br>@@ -217,7 +217,12 @@<br><br> def invoke<br> @invoked += 1<br>- yield(*@parameters_to_yield) if yield? and block_given?
<br>+ if yield? and block_given?<br>+ params = @parameters_to_yield.collect do |element|<br>+ element.is_a?(Proc) ? element.call : element<br>+ end<br>+ yield(*params)<br>+ end<br> @return_value.is_a?(Proc) ? @return_value.call : @return_value
<br> end<br><br>This test passes also, but I didn't think it was necessary for the<br>suite...<br> def test_should_yield_with_block_result_1123<br> expected_vals = [1,2,3]<br> expectation = new_expectation.yields( lambda
<br>{ expected_vals.shift }, 4 )<br> expectation.invoke() { |*parameters| assert_equal [1, 4],<br>parameters }<br> expectation.invoke() { |*parameters| assert_equal [2, 4],<br>parameters }<br> expectation.invoke
() { |*parameters| assert_equal [3, 4],<br>parameters }<br> end<br><br><br>Along the same thread, I thought about using a comma separated list<br>(similar to the new returns format); however, it wont work since the<br>parameters are captured as args and returned as one value.
<br><br>Thoughts?</blockquote><div><br>Thanks for your patch. I'm sorry not to have replied sooner - works been busy and then I was off sick.<br><br>I can see the need for <span style="font-family: courier new,monospace;">
yields()</span> to accept a <span style="font-family: courier new,monospace;">Proc</span> in the same way that <span style="font-family: courier new,monospace;">returns()</span> does. However, I'm not totally convinced that accepting multiple
<span style="font-family: courier new,monospace;">Proc</span>s actually gains us anything - could you not rewrite your example test like this...<br><br> def test_should_yield_with_block_result_1123<br> expected_vals = [1,2,3]
<br> expectation = new_expectation.yields( lambda { [expected_vals.shift, 4] } )<br> expectation.invoke() { |*parameters| assert_equal [1, 4], parameters }<br> expectation.invoke() { |*parameters| assert_equal [2, 4], parameters }
<br> expectation.invoke() { |*parameters| assert_equal [3, 4], parameters }<br> end<br><br>I'm trying to decide which is the most consistent / unsurprising approach.<br><br>What do you think?<br></div></div>-- <br>James.
<br><a href="http://blog.floehopper.org">http://blog.floehopper.org</a>