Skip to content

Commit

Permalink
Correct error with have_received counts
Browse files Browse the repository at this point in the history
* add triangulation example for have_received error
* rescue RSpec::Mocks::MockExpectationError when replaying expectations,
  we need to count them all.
  • Loading branch information
JonRowe committed Oct 1, 2014
1 parent bc221e4 commit edb39a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/rspec/mocks/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ def replay_received_message_on(expectation, &block)
@messages_received.each do |(actual_method_name, args, _)|
next unless expectation.matches?(actual_method_name, *args)

expectation.invoke(nil)
# rubocop:disable Lint/HandleExceptions
begin
expectation.invoke(nil)
rescue RSpec::Mocks::MockExpectationError
# carry on, our expectation failure is handled later
end
# rubocop:enable Lint/HandleExceptions
block.call(*args) if block
end
end
Expand Down
10 changes: 9 additions & 1 deletion spec/rspec/mocks/matchers/have_received_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ module Mocks
expect(dbl).to have_received(:expected_method).exactly(3).times
end

it 'fails when the message was received more times' do
it 'fails when the message was received more times than expected' do
expect {
expect(dbl).to have_received(:expected_method).exactly(1).times
}.to raise_error(/expected: 1 time.*received: 3 times/m)
expect {
expect(dbl).to have_received(:expected_method).exactly(2).times
}.to raise_error(/expected: 2 times.*received: 3 times/m)
expect {
dbl.expected_method
dbl.expected_method
expect(dbl).to have_received(:expected_method).exactly(2).times
}.to raise_error(/expected: 2 times.*received: 5 times/m)
end

it 'fails when the message was received fewer times' do
Expand Down

0 comments on commit edb39a8

Please sign in to comment.