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

Commit 59a4464

Browse files
committed
Cleanup formatter support a bit.
- Stop memoizing `example`. It made it difficult to construct multiple different examples. - Explicitly create the examples where they are needed.
1 parent 9560970 commit 59a4464

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

spec/rspec/core/notifications_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
RSpec.describe "FailedExampleNotification" do
55
include FormatterSupport
66

7+
let(:example) { new_example }
78
let(:notification) { ::RSpec::Core::Notifications::FailedExampleNotification.new(example) }
89

910
before do
11+
allow(example.execution_result).to receive(:exception) { exception }
1012
example.metadata[:absolute_file_path] = __FILE__
1113
end
1214

spec/rspec/core/reporter_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module RSpec::Core
6666
it "passes messages to that formatter" do
6767
formatter = double("formatter", :example_started => nil)
6868
reporter.register_listener formatter, :example_started
69+
example = new_example
6970

7071
expect(formatter).to receive(:example_started) do |notification|
7172
expect(notification.example).to eq example
@@ -121,6 +122,7 @@ module RSpec::Core
121122
context "given multiple formatters" do
122123
it "passes messages to all formatters" do
123124
formatters = (1..2).map { double("formatter", :example_started => nil) }
125+
example = new_example
124126

125127
formatters.each do |formatter|
126128
expect(formatter).to receive(:example_started) do |notification|

spec/support/formatter_support.rb

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -200,34 +200,28 @@ def formatter
200200
end
201201
end
202202

203-
def example
204-
@example ||=
205-
begin
206-
result = instance_double(RSpec::Core::Example::ExecutionResult,
207-
:pending_fixed? => false,
208-
:example_skipped? => false,
209-
:status => :passed
210-
)
211-
allow(result).to receive(:exception) { exception }
212-
instance_double(RSpec::Core::Example,
213-
:description => "Example",
214-
:full_description => "Example",
215-
:execution_result => result,
216-
:location => "",
217-
:location_rerun_argument => "",
218-
:metadata => {
219-
:shared_group_inclusion_backtrace => []
220-
}
221-
)
222-
end
223-
end
224-
225-
def exception
226-
Exception.new
203+
def new_example(metadata = {})
204+
result = instance_double(RSpec::Core::Example::ExecutionResult,
205+
:pending_fixed? => false,
206+
:example_skipped? => false,
207+
:status => :passed,
208+
:exception => Exception.new
209+
)
210+
211+
instance_double(RSpec::Core::Example,
212+
:description => "Example",
213+
:full_description => "Example",
214+
:execution_result => result,
215+
:location => "",
216+
:location_rerun_argument => "",
217+
:metadata => {
218+
:shared_group_inclusion_backtrace => []
219+
}.merge(metadata)
220+
)
227221
end
228222

229223
def examples(n)
230-
(1..n).map { example }
224+
Array.new(n) { new_example }
231225
end
232226

233227
def group
@@ -242,7 +236,7 @@ def stop_notification
242236
::RSpec::Core::Notifications::ExamplesNotification.new reporter
243237
end
244238

245-
def example_notification(specific_example = example)
239+
def example_notification(specific_example = new_example)
246240
::RSpec::Core::Notifications::ExampleNotification.for specific_example
247241
end
248242

0 commit comments

Comments
 (0)