From 27d6a72e260191c6bb00c76d9ad8d83b61d32c9b Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 31 Oct 2019 20:20:27 +0000 Subject: [PATCH] Change method dispatch behaviour to that of jMock v2 The old behavior modelled on the method dispatch behaviour of jMock v1 caused slightly confusing expectation behaviour when stubbing after expectation --- lib/mocha/expectation_list.rb | 2 +- .../mocked_methods_dispatch_test.rb | 48 +++++++++---------- ...tiple_expectations_failure_message_test.rb | 4 +- test/unit/expectation_list_test.rb | 4 +- test/unit/mock_test.rb | 16 +++---- 5 files changed, 37 insertions(+), 37 deletions(-) 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/test/acceptance/mocked_methods_dispatch_test.rb b/test/acceptance/mocked_methods_dispatch_test.rb index 9d604edb7..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) + mock.stubs(:method).returns(1) + mock.expects(:method).returns(2) 3.times { responses << mock.method } end - assert_equal [2, 2, 2], responses + 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