Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change method dispatch behavior to that of jmock2 #395

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
nitishr committed Dec 24, 2019
commit 27d6a72e260191c6bb00c76d9ad8d83b61d32c9b
2 changes: 1 addition & 1 deletion lib/mocha/expectation_list.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ def initialize(expectations = [])
end

def add(expectation)
@expectations.unshift(expectation)
@expectations.push(expectation)
expectation
end

48 changes: 24 additions & 24 deletions test/acceptance/mocked_methods_dispatch_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/acceptance/multiple_expectations_failure_message_test.rb
Original file line number Diff line number Diff line change
@@ -59,8 +59,8 @@ def test_should_report_multiple_satisfied_expectations
'unsatisfied expectations:',
'- expected exactly 3 times, invoked twice: #<Mock:mock>.method_three(any_parameters)',
'satisfied expectations:',
'- expected exactly twice, invoked twice: #<Mock:mock>.method_two(any_parameters)',
'- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)'
'- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)',
'- expected exactly twice, invoked twice: #<Mock:mock>.method_two(any_parameters)'
], test_result.failure_message_lines
end
end
4 changes: 2 additions & 2 deletions test/unit/expectation_list_test.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions test/unit/mock_test.rb
Original file line number Diff line number Diff line change
@@ -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