diff --git a/README.md b/README.md index 1473c3581..38218973b 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,7 @@ Maybe, but probably not. Partial mocking changes the state of objects in the `Ob Stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The `Expectation#stubs` method is syntactic sugar to make the intent of the test more explicit. -When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. +When a method is invoked on a mock object, the Mockery searches through its expectations from oldest to newest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. See the [documentation](https://mocha.jamesmead.org/Mocha/Mock.html) for `Mocha::Mock` for further details. diff --git a/lib/mocha/expectation_list.rb b/lib/mocha/expectation_list.rb index f5366ec17..4b7acab01 100644 --- a/lib/mocha/expectation_list.rb +++ b/lib/mocha/expectation_list.rb @@ -5,7 +5,7 @@ def initialize(expectations = []) end def add(expectation) - @expectations.unshift(expectation) + @expectations.push(expectation) expectation end diff --git a/lib/mocha/mock.rb b/lib/mocha/mock.rb index ff14c0ce7..fefee1642 100644 --- a/lib/mocha/mock.rb +++ b/lib/mocha/mock.rb @@ -27,34 +27,27 @@ module Mocha # expectation of zero or more invocations. The {#stubs} method is syntactic # sugar to make the intent of the test more explicit. # - # When a method is invoked on a mock object, the mock object searches through - # its expectations from newest to oldest to find one that matches the + # When a method is invoked on a mock object, the Mockery searches through + # its expectations from oldest to newest to find one that matches the # invocation. After the invocation, the matching expectation might stop # matching further invocations. For example, an +expects(:foo).once+ # expectation only matches once and will be ignored on future invocations # while an +expects(:foo).at_least_once+ expectation will always be matched # against invocations. # - # This scheme allows you to: - # - # - Set up default stubs in your the +setup+ method of your test class and - # override some of those stubs in individual tests. - # - Set up different +once+ expectations for the same method with different - # action per invocation. However, it's better to use the + # This scheme allows you to set up different expectations for the same method + # with different action per invocation. However, it's better to use the # {Expectation#returns} method with multiple arguments to do this, as # described below. # # However, there are some possible "gotchas" caused by this scheme: # - # - if you create an expectation and then a stub for the same method, the + # - if you create a stub and then an expectation for the same method, the # stub will always override the expectation and the expectation will never # be met. - # - if you create a stub and then an expectation for the same method, the + # - if you create an expectation and then a stub for the same method, the # expectation will match, and when it stops matching the stub will be used # instead, possibly masking test failures. - # - if you create different expectations for the same method, they will be - # invoked in the opposite order than that in which they were specified, - # rather than the same order. # # The best thing to do is not set up multiple expectations and stubs for the # same method with exactly the same matchers. Instead, use the diff --git a/test/acceptance/mocked_methods_dispatch_test.rb b/test/acceptance/mocked_methods_dispatch_test.rb index 9b53aa992..c4064acdc 100644 --- a/test/acceptance/mocked_methods_dispatch_test.rb +++ b/test/acceptance/mocked_methods_dispatch_test.rb @@ -11,64 +11,64 @@ def teardown teardown_acceptance_test end - def test_should_find_latest_matching_expectation + def test_should_find_oldest_matching_expectation test_result = run_as_test do mock = mock() mock.stubs(:method).returns(1) mock.stubs(:method).returns(2) - assert_equal 2, mock.method - assert_equal 2, mock.method - assert_equal 2, mock.method + assert_equal 1, mock.method + assert_equal 1, mock.method + assert_equal 1, mock.method end assert_passed(test_result) end - def test_should_find_latest_expectation_which_has_not_stopped_matching + def test_should_find_oldest_expectation_which_has_not_stopped_matching test_result = run_as_test do mock = mock() - mock.stubs(:method).returns(1) - mock.stubs(:method).once.returns(2) - assert_equal 2, mock.method - assert_equal 1, mock.method + mock.stubs(:method).once.returns(1) + mock.stubs(:method).returns(2) assert_equal 1, mock.method + assert_equal 2, mock.method + assert_equal 2, mock.method end assert_passed(test_result) end - def test_should_keep_finding_later_stub_and_so_never_satisfy_earlier_expectation + def test_should_keep_finding_older_stub_and_so_never_satisfy_earlier_expectation + responses = [] test_result = run_as_test do mock = mock() - mock.expects(:method).returns(1) - mock.stubs(:method).returns(2) - assert_equal 2, mock.method - assert_equal 2, mock.method - assert_equal 2, mock.method + mock.stubs(:method).returns(1) + mock.expects(:method).returns(2) + 3.times { responses << mock.method } end + assert_equal [1, 1, 1], responses assert_failed(test_result) end - def test_should_find_later_expectation_until_it_stops_matching_then_find_earlier_stub + def test_should_find_older_expectation_until_it_stops_matching_then_find_earlier_stub test_result = run_as_test do mock = mock() - mock.stubs(:method).returns(1) - mock.expects(:method).returns(2) - assert_equal 2, mock.method - assert_equal 1, mock.method + mock.expects(:method).returns(1) + mock.stubs(:method).returns(2) assert_equal 1, mock.method + assert_equal 2, mock.method + assert_equal 2, mock.method end assert_passed(test_result) end - def test_should_find_latest_expectation_with_range_of_expected_invocation_count_which_has_not_stopped_matching + def test_should_find_oldest_expectation_with_range_of_expected_invocation_count_which_has_not_stopped_matching test_result = run_as_test do mock = mock() - mock.stubs(:method).returns(1) - mock.stubs(:method).times(2..3).returns(2) - assert_equal 2, mock.method - assert_equal 2, mock.method - assert_equal 2, mock.method + mock.stubs(:method).times(2..3).returns(1) + mock.stubs(:method).returns(2) + assert_equal 1, mock.method assert_equal 1, mock.method assert_equal 1, mock.method + assert_equal 2, mock.method + assert_equal 2, mock.method end assert_passed(test_result) end diff --git a/test/acceptance/multiple_expectations_failure_message_test.rb b/test/acceptance/multiple_expectations_failure_message_test.rb index d25ec721e..2e45b5d4e 100644 --- a/test/acceptance/multiple_expectations_failure_message_test.rb +++ b/test/acceptance/multiple_expectations_failure_message_test.rb @@ -59,8 +59,8 @@ def test_should_report_multiple_satisfied_expectations 'unsatisfied expectations:', '- expected exactly 3 times, invoked twice: #.method_three(any_parameters)', 'satisfied expectations:', - '- expected exactly twice, invoked twice: #.method_two(any_parameters)', - '- expected exactly once, invoked once: #.method_one(any_parameters)' + '- expected exactly once, invoked once: #.method_one(any_parameters)', + '- expected exactly twice, invoked twice: #.method_two(any_parameters)' ], test_result.failure_message_lines end end diff --git a/test/unit/expectation_list_test.rb b/test/unit/expectation_list_test.rb index 20a0de334..a4ec6b933 100644 --- a/test/unit/expectation_list_test.rb +++ b/test/unit/expectation_list_test.rb @@ -38,13 +38,13 @@ def test_should_remove_all_expectations_matching_method_name assert_same expectation3, expectation_list.match(Invocation.new(:irrelevant, :method_two)) end - def test_should_find_most_recent_matching_expectation + def test_should_find_oldest_matching_expectation expectation_list = ExpectationList.new expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2) expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2) expectation_list.add(expectation1) expectation_list.add(expectation2) - assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2)) + assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2)) end def test_should_find_matching_expectation_allowing_invocation diff --git a/test/unit/mock_test.rb b/test/unit/mock_test.rb index ed31db5ce..1f503496a 100644 --- a/test/unit/mock_test.rb +++ b/test/unit/mock_test.rb @@ -200,32 +200,32 @@ def test_should_keep_returning_specified_value_for_expects assert_equal 1, mock.method1 end - def test_should_match_most_recent_call_to_expects + def test_should_match_oldest_call_to_expects mock = build_mock mock.expects(:method1).returns(0) mock.expects(:method1).returns(1) - assert_equal 1, mock.method1 + assert_equal 0, mock.method1 end - def test_should_match_most_recent_call_to_stubs + def test_should_match_oldest_call_to_stubs mock = build_mock mock.stubs(:method1).returns(0) mock.stubs(:method1).returns(1) - assert_equal 1, mock.method1 + assert_equal 0, mock.method1 end - def test_should_match_most_recent_call_to_stubs_or_expects + def test_should_match_oldest_call_to_stubs_or_expects mock = build_mock mock.stubs(:method1).returns(0) mock.expects(:method1).returns(1) - assert_equal 1, mock.method1 + assert_equal 0, mock.method1 end - def test_should_match_most_recent_call_to_expects_or_stubs + def test_should_match_oldest_call_to_expects_or_stubs mock = build_mock mock.expects(:method1).returns(0) mock.stubs(:method1).returns(1) - assert_equal 1, mock.method1 + assert_equal 0, mock.method1 end def test_should_respond_to_expected_method