[mocha-developer] foo.expects(:blah).returns(10).then(11) syntax
Jay
jay at jayfields.com
Fri Sep 22 08:39:02 EDT 2006
I recently needed the behavior of my object to change on subsequent
method calls so I added the syntax above.
Here's the diff of changes I made:
Index: test/mocha/expectation_test.rb
===================================================================
--- test/mocha/expectation_test.rb (revision 854)
+++ test/mocha/expectation_test.rb (working copy)
@@ -95,6 +95,12 @@
assert_equal 99, expectation.invoke
end
+ def test_should_return_values_in_order
+ expectation = Expectation.new(:expected_method).returns(9).then(10)
+ assert_equal 9, expectation.invoke
+ assert_equal 10, expectation.invoke
+ end
+
def test_should_return_nil_if_no_value_specified
expectation = Expectation.new(:expected_method)
assert_nil expectation.invoke
Index: lib/mocha/expectation.rb
===================================================================
--- lib/mocha/expectation.rb (revision 854)
+++ lib/mocha/expectation.rb (working copy)
@@ -21,7 +21,7 @@
@method_name = method_name
@count = 1
@parameters, @parameter_block = AlwaysEqual.new, nil
- @invoked, @return_value = 0, nil
+ @invoked = 0
@backtrace = backtrace || caller
end
@@ -69,19 +69,29 @@
end
def returns(value)
- @return_value = value
+ return_value << value
self
end
+
+ def then(value)
+ return_value << value
+ self
+ end
+
+ def return_value
+ @return_value ||= []
+ end
def raises(exception = RuntimeError, message = nil)
- @return_value = lambda{ raise exception, message }
+ return_value << lambda{ raise exception, message }
self
end
def invoke
@invoked += 1
yield(*@parameters_to_yield) if yield? and block_given?
- @return_value.is_a?(Proc) ? @return_value.call : @return_value
+ this_return = return_value.size > 1 ? return_value.shift :
return_value.first
+ this_return.is_a?(Proc) ? this_return.call : this_return
end
def verify
More information about the mocha-developer
mailing list