On 22/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 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 (revision 854)<br>+++ test/mocha/expectation_test.rb (working copy)<br>@@ -95,6 +95,12 @@<br> assert_equal 99, expectation.invoke<br> end<br><br>+ def test_should_return_values_in_order
<br>+ expectation = Expectation.new(:expected_method).returns(9).then(10)<br>+ assert_equal 9, expectation.invoke<br>+ assert_equal 10, expectation.invoke<br>+ end<br>+<br> def test_should_return_nil_if_no_value_specified
<br> expectation = Expectation.new(:expected_method)<br> assert_nil expectation.invoke<br>Index: lib/mocha/expectation.rb<br>===================================================================<br>--- lib/mocha/expectation.rb (revision 854)
<br>+++ lib/mocha/expectation.rb (working copy)<br>@@ -21,7 +21,7 @@<br> @method_name = method_name<br> @count = 1<br> @parameters, @parameter_block = AlwaysEqual.new, nil<br>- @invoked, @return_value = 0, nil
<br>+ @invoked = 0<br> @backtrace = backtrace || caller<br> end<br><br>@@ -69,19 +69,29 @@<br> end<br> def returns(value)<br>- @return_value = value<br>+ return_value << value<br>
self<br> end<br>+<br>+ def then(value)<br>+ return_value << value<br>+ self<br>+ end<br>+<br>+ def return_value<br>+ @return_value ||= []<br>+ end<br><br> def raises(exception = RuntimeError, message = nil)
<br>- @return_value = lambda{ raise exception, message }<br>+ return_value << lambda{ raise exception, message }<br> self<br> end<br> def invoke<br> @invoked += 1<br> yield(*@parameters_to_yield) if yield? and block_given?
<br>- @return_value.is_a?(Proc) ? @return_value.call : @return_value<br>+ this_return = return_value.size > 1 ? return_value.shift :<br>return_value.first<br>+ this_return.is_a?(Proc) ? this_return.call : this_return
<br> end<br> 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;"> object = mock()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> object.expects(:expected_method).returns(:result1, :result2, :result3)
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> object.expected_method
# => :result1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> object.expected_method # => :result2</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
object.expected_method # => :result3</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> object.expected_method # => 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>