Permalink
Browse files

method twice expectation.

  • Loading branch information...
1 parent 911d458 commit b056bc5f05b4f734ed604ab66a9e93aa0d715360 @tinogomes tinogomes committed Jan 29, 2009
Showing with 56 additions and 0 deletions.
  1. +30 −0 lib/mocha/expectation.rb
  2. +26 −0 test/unit/expectation_test.rb
View
30 lib/mocha/expectation.rb
@@ -66,6 +66,36 @@ def once
self
end
+ # :call-seq: twice() -> expectation
+ #
+ # Modifies expectation so that the expected method must be called exactly twice.
+ # Note that this is the default behaviour for an expectation, but you may wish to use it for clarity/emphasis.
+ # object = mock()
+ # object.expects(:expected_method).twice
+ # object.expected_method
+ # object.expected_method
+ # # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).twice
+ # object.expected_method
+ # object.expected_method
+ # object.expected_method
+ # # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).twice
+ # object.expected_method
+ # # => verify fails
+ #
+ # object = mock()
+ # object.expects(:expected_method).twice
+ # # => verify fails
+ def twice
+ @cardinality = Cardinality.exactly(2)
+ self
+ end
+
# :call-seq: never() -> expectation
#
# Modifies expectation so that the expected method must never be called.
View
26 test/unit/expectation_test.rb
@@ -253,6 +253,32 @@ def test_should_verify_successfully_if_call_expected_once_and_invoked_once
assert expectation.verified?
end
+ def test_should_verify_successfully_if_call_expected_twice_and_invoked_three_times
+ expectation = new_expectation.once
+ expectation.invoke
+ expectation.invoke
+ expectation.invoke
+ assert !expectation.verified?
+ end
+
+ def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
+ expectation = new_expectation.twice
+ expectation.invoke
+ assert !expectation.verified?
+ end
+
+ def test_should_not_verify_successfully_if_call_expected_twice_but_not_invoked
+ expectation = new_expectation.twice
+ assert !expectation.verified?
+ end
+
+ def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
+ expectation = new_expectation.twice
+ expectation.invoke
+ expectation.invoke
+ assert expectation.verified?
+ end
+
def test_should_verify_successfully_if_expected_call_was_made_at_least_once
expectation = new_expectation.at_least_once
3.times {expectation.invoke}

0 comments on commit b056bc5

Please sign in to comment.