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

Commit 1d86a8b

Browse files
authored
Merge pull request #2291 from rspec/myron/fix-bug-in-example-finished
Fill in execution_status before reporting example_finished.
2 parents f2bb06a + c0564a3 commit 1d86a8b

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Enhancements:
66
* Warn when duplicate shared exmaples definitions are loaded due to being
77
defined in files matching the spec pattern (e.g. `_spec.rb`) (#2278, Devon Estes)
88

9+
Bug Fixes:
10+
11+
* Wait to report `example_finished` until the example's `execution_result`
12+
has been completely filled in. (Myron Marston, #2291)
13+
914
### 3.5.1 / 2016-07-06
1015
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.5.0...v3.5.1)
1116

lib/rspec/core/example.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,14 @@ def start(reporter)
461461
def finish(reporter)
462462
pending_message = execution_result.pending_message
463463

464-
reporter.example_finished(self)
465464
if @exception
466-
record_finished :failed
467465
execution_result.exception = @exception
466+
record_finished :failed
468467
reporter.example_failed self
469468
false
470469
elsif pending_message
471-
record_finished :pending
472470
execution_result.pending_message = pending_message
471+
record_finished :pending
473472
reporter.example_pending self
474473
true
475474
else
@@ -481,6 +480,7 @@ def finish(reporter)
481480

482481
def record_finished(status)
483482
execution_result.record_finished(status, clock.now)
483+
reporter.example_finished(self)
484484
end
485485

486486
def run_before_example

spec/rspec/core/example_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,70 @@ def expect_gc(opts)
555555
end
556556
end
557557

558+
describe "reporting example_finished" do
559+
let(:reporter) { RSpec::Core::Reporter.new(RSpec::Core::Configuration.new) }
560+
561+
def capture_reported_execution_result_for_example(&block)
562+
reporter = RSpec::Core::Reporter.new(RSpec::Core::Configuration.new)
563+
564+
reported_execution_result = nil
565+
566+
listener = double("Listener")
567+
allow(listener).to receive(:example_finished) do |notification|
568+
reported_execution_result = notification.example.execution_result.dup
569+
end
570+
571+
reporter.register_listener(listener, :example_finished)
572+
573+
RSpec.describe do
574+
it(&block)
575+
end.run(reporter)
576+
577+
reported_execution_result
578+
end
579+
580+
it "fills in the execution result details before reporting a passed example as finished" do
581+
execution_result = capture_reported_execution_result_for_example do
582+
expect(1).to eq 1
583+
end
584+
585+
expect(execution_result).to have_attributes(
586+
:status => :passed,
587+
:exception => nil,
588+
:finished_at => a_value_within(1).of(Time.now),
589+
:run_time => a_value >= 0
590+
)
591+
end
592+
593+
it "fills in the execution result details before reporting a failed example as finished" do
594+
execution_result = capture_reported_execution_result_for_example do
595+
expect(1).to eq 2
596+
end
597+
598+
expect(execution_result).to have_attributes(
599+
:status => :failed,
600+
:exception => RSpec::Expectations::ExpectationNotMetError,
601+
:finished_at => a_value_within(1).of(Time.now),
602+
:run_time => a_value >= 0
603+
)
604+
end
605+
606+
it "fills in the execution result details before reporting a pending example as finished" do
607+
execution_result = capture_reported_execution_result_for_example do
608+
pending "because"
609+
expect(1).to eq 2
610+
end
611+
612+
expect(execution_result).to have_attributes(
613+
:status => :pending,
614+
:pending_message => "because",
615+
:pending_exception => RSpec::Expectations::ExpectationNotMetError,
616+
:finished_at => a_value_within(1).of(Time.now),
617+
:run_time => a_value >= 0
618+
)
619+
end
620+
end
621+
558622
describe "#pending" do
559623
def expect_pending_result(example)
560624
expect(example).to be_pending

0 commit comments

Comments
 (0)