Skip to content

Commit

Permalink
WIP: Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Nov 27, 2024
1 parent 15985bc commit d74c4c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/mocha/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,22 @@ def handle_method_call(symbol, arguments, block)
invocation = Invocation.new(self, symbol, arguments, block)

matching_expectations = all_expectations.matching_expectations(invocation)
matching_expectation_allowing_invocation = matching_expectations.detect(&:invocations_allowed?)
matching_expectation_never_allowing_invocation = matching_expectations.detect(&:invocations_never_allowed?)

if matching_expectation_allowing_invocation && !matching_expectation_never_allowing_invocation
matching_expectation_allowing_invocation.invoke(invocation)
else
matching_expectation_ignoring_order = all_expectations.match(invocation, ignoring_order: true)
if matching_expectation_ignoring_order || (!matching_expectation_ignoring_order && !@everything_stubbed)
raise_unexpected_invocation_error(invocation, matching_expectation_ignoring_order)
index = 0
while index < matching_expectations.length
matching_expectation = matching_expectations[index]
if matching_expectation.invocations_never_allowed?
raise_unexpected_invocation_error(invocation, matching_expectation)
elsif matching_expectation.invocations_allowed?
return matching_expectation.invoke(invocation)
end
index += 1
end

matching_expectation_ignoring_order = all_expectations.match(invocation, ignoring_order: true)
return if !matching_expectation_ignoring_order || @everything_stubbed

raise_unexpected_invocation_error(invocation, matching_expectation_ignoring_order)
end

# @private
Expand Down
10 changes: 10 additions & 0 deletions test/acceptance/mocked_methods_dispatch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ def test_should_fail_fast_if_invocation_matches_expectation_with_never_cardinali
'- allowed any number of times, invoked never: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end

def test_should_not_fail_fast_if_invocation_matches_expectation_allowing_invocation_before_matching_expectation_with_never_cardinality
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).never
mock.expects(:method).once
mock.method
end
assert_passed(test_result)
end
end

0 comments on commit d74c4c6

Please sign in to comment.