On 22/09/06, <b class="gmail_sendername">Jay</b> &lt;<a href="mailto:jay@jayfields.com">jay@jayfields.com</a>&gt; 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 recently needed the behavior of my object to change on subsequent<br>method calls so I added the syntax above.<br><br>Here's the diff of changes I made:<br><br>Index: test/mocha/expectation_test.rb<br>===================================================================
<br>--- test/mocha/expectation_test.rb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(revision 854)<br>+++ test/mocha/expectation_test.rb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(working copy)<br>@@ -95,6 +95,12 @@<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert_equal 99, expectation.invoke<br>&nbsp;&nbsp;&nbsp;&nbsp;end<br><br>+&nbsp;&nbsp;def test_should_return_values_in_order
<br>+&nbsp;&nbsp;&nbsp;&nbsp;expectation = Expectation.new(:expected_method).returns(9).then(10)<br>+&nbsp;&nbsp;&nbsp;&nbsp;assert_equal 9, expectation.invoke<br>+&nbsp;&nbsp;&nbsp;&nbsp;assert_equal 10, expectation.invoke<br>+&nbsp;&nbsp;end<br>+<br>&nbsp;&nbsp;&nbsp;&nbsp;def test_should_return_nil_if_no_value_specified
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expectation = Expectation.new(:expected_method)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert_nil expectation.invoke<br>Index: lib/mocha/expectation.rb<br>===================================================================<br>--- lib/mocha/expectation.rb&nbsp;&nbsp;&nbsp;&nbsp;(revision 854)
<br>+++ lib/mocha/expectation.rb&nbsp;&nbsp;&nbsp;&nbsp;(working copy)<br>@@ -21,7 +21,7 @@<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@method_name = method_name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@count = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@parameters, @parameter_block = AlwaysEqual.new, nil<br>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invoked, @return_value = 0, nil
<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invoked = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@backtrace = backtrace || caller<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br><br>@@ -69,19 +69,29 @@<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def returns(value)<br>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return_value = value<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return_value &lt;&lt; value<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>+<br>+&nbsp;&nbsp;&nbsp;&nbsp;def then(value)<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return_value &lt;&lt; value<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<br>+&nbsp;&nbsp;&nbsp;&nbsp;end<br>+<br>+&nbsp;&nbsp;&nbsp;&nbsp;def return_value<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return_value ||= []<br>+&nbsp;&nbsp;&nbsp;&nbsp;end<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def raises(exception = RuntimeError, message = nil)
<br>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return_value = lambda{ raise exception, message }<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return_value &lt;&lt; lambda{ raise exception, message }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def invoke<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invoked += 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(*@parameters_to_yield) if yield? and block_given?
<br>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return_value.is_a?(Proc) ? @return_value.call : @return_value<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this_return = return_value.size &gt; 1 ? return_value.shift :<br>return_value.first<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this_return.is_a?(Proc) ? this_return.call : this_return
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def verify<br></blockquote></div><br>Hi Jay,<br><br>Thanks for your patch and in particular for including a test. Interestingly I recently made a change (revision #55) to allow consecutive return values to be specified like this...
<br><br><span style="font-family: courier new,monospace;">&nbsp; object = mock()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; object.expects(:expected_method).returns(:result1, :result2, :result3)
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; object.expected_method
 # =&gt; :result1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; object.expected_method # =&gt; :result2</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
&nbsp; object.expected_method # =&gt; :result3</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; object.expected_method # =&gt; nil</span><br style="font-family: courier new,monospace;">
<br>I wonder if this would be sufficient for your needs. Let me know what you think.<br><br>I hope to do a release in the next few days.<br clear="all"><br>Thanks.<br>-- <br>James.<br><a href="http://blog.floehopper.org">
http://blog.floehopper.org</a>