Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit f989168

Browse files
committed
Always verify mocks at the end of each example.
...even if the example has already failed. Previously, we did not have a good way to display multiple failures for a single example, but we do now, so it’s simpler and more consistent to always verify, and to provide the user with multiple exceptions if there are multiple.
1 parent 993b796 commit f989168

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

lib/rspec/core/example.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def run_after_example
408408
end
409409

410410
def verify_mocks
411-
@example_group_instance.verify_mocks_for_rspec if mocks_need_verification?
411+
@example_group_instance.verify_mocks_for_rspec
412412
rescue Exception => e
413413
if pending?
414414
execution_result.pending_fixed = false
@@ -419,10 +419,6 @@ def verify_mocks
419419
end
420420
end
421421

422-
def mocks_need_verification?
423-
exception.nil? || execution_result.pending_fixed?
424-
end
425-
426422
def assign_generated_description
427423
if metadata[:description].empty? && (description = generate_description)
428424
metadata[:description] = description

spec/rspec/core/example_spec.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -723,32 +723,35 @@ def expect_pending_result(example)
723723
expect(ex).to fail_with(RSpec::Mocks::MockExpectationError)
724724
end
725725

726-
it 'skips mock verification if the example has already failed' do
727-
ex = nil
728-
boom = StandardError.new("boom")
729-
730-
RSpec.describe do
731-
ex = example do
732-
dbl = double
733-
expect(dbl).to receive(:Foo)
734-
raise boom
735-
end
736-
end.run
726+
context "when the example has already failed" do
727+
it 'appends the mock error to a `MultipleExceptionError` so the user can see both' do
728+
ex = nil
729+
boom = StandardError.new("boom")
730+
731+
RSpec.describe do
732+
ex = example do
733+
dbl = double
734+
expect(dbl).to receive(:Foo)
735+
raise boom
736+
end
737+
end.run
737738

738-
expect(ex.exception).to be boom
739+
expect(ex.exception).to be_a(RSpec::Core::MultipleExceptionError)
740+
expect(ex.exception.all_exceptions).to match [boom, an_instance_of(RSpec::Mocks::MockExpectationError)]
741+
end
739742
end
740743

741744
it 'allows `after(:example)` hooks to satisfy mock expectations, since examples are not complete until their `after` hooks run' do
742745
ex = nil
743746

744747
RSpec.describe do
745-
let(:dbl) { double }
748+
let(:the_dbl) { double }
746749

747750
ex = example do
748-
expect(dbl).to receive(:foo)
751+
expect(the_dbl).to receive(:foo)
749752
end
750753

751-
after { dbl.foo }
754+
after { the_dbl.foo }
752755
end.run
753756

754757
expect(ex).to pass

0 commit comments

Comments
 (0)