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

Commit 9d12a30

Browse files
committed
Merge pull request #1866 from rspec/expose_reporter_and_allow_message
Expose reporter to running examples
2 parents 703e742 + 2633182 commit 9d12a30

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### Development
2+
3+
Enhancements:
4+
5+
* Expose the reporter used to run examples via `RSpec::Core::Example#reporter`.
6+
(Jon Rowe, #1866)
7+
* Make `RSpec::Core::Reporter#message` a public supported API. (Jon Rowe, #1866)
8+
19
### 3.2.0 / 2015-02-03
210
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.1.7...v3.2.0)
311

lib/rspec/core/example.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ def initialize(example_group_class, description, user_metadata, example_block=ni
143143

144144
@example_group_instance = @exception = nil
145145
@clock = RSpec::Core::Time
146+
@reporter = RSpec::Core::NullReporter
146147
end
147148

149+
# @return [RSpec::Core::Reporter] the current reporter for the example
150+
attr_reader :reporter
151+
148152
# Returns the example group class that provides the context for running
149153
# this example.
150154
def example_group
@@ -160,6 +164,7 @@ def example_group
160164
# @param example_group_instance the instance of an ExampleGroup subclass
161165
def run(example_group_instance, reporter)
162166
@example_group_instance = example_group_instance
167+
@reporter = reporter
163168
hooks.register_global_singleton_context_hooks(self, RSpec.configuration.hooks)
164169
RSpec.configuration.configure_example(self)
165170
RSpec.current_example = self

lib/rspec/core/example_group.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def self.run_after_context_hooks(example_group_instance)
499499
end
500500

501501
# Runs all the examples in this group.
502-
def self.run(reporter=RSpec::Core::NullReporter.new)
502+
def self.run(reporter=RSpec::Core::NullReporter)
503503
if RSpec.world.wants_to_quit
504504
RSpec.world.clear_remaining_example_groups if top_level?
505505
return

lib/rspec/core/reporter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def registered_listeners(notification)
4040
@listeners[notification].to_a
4141
end
4242

43-
# @api
4443
# @overload report(count, &block)
4544
# @overload report(count, &block)
4645
# @param expected_example_count [Integer] the number of examples being run
@@ -73,7 +72,9 @@ def start(expected_example_count, time=RSpec::Core::Time.now)
7372
notify :seed, Notifications::SeedNotification.new(@configuration.seed, seed_used?)
7473
end
7574

76-
# @private
75+
# @param message [#to_s] A message object to send to formatters
76+
#
77+
# Send a custom message to supporting formatters.
7778
def message(message)
7879
notify :message, Notifications::MessageNotification.new(message)
7980
end
@@ -163,10 +164,9 @@ def seed_used?
163164
# @private
164165
# # Used in place of a {Reporter} for situations where we don't want reporting output.
165166
class NullReporter
166-
private
167-
168-
def method_missing(*)
167+
def self.method_missing(*)
169168
# ignore
170169
end
170+
private_class_method :method_missing
171171
end
172172
end

spec/rspec/core/example_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,19 @@ def expect_pending_result(example)
732732
expect(ex).to pass
733733
end
734734
end
735+
736+
describe "exposing the examples reporter" do
737+
it "returns a null reporter when the example hasnt run yet" do
738+
example = RSpec.describe.example
739+
expect(example.reporter).to be RSpec::Core::NullReporter
740+
end
741+
742+
it "returns the reporter used to run the example when executed" do
743+
reporter = double(:reporter).as_null_object
744+
group = RSpec.describe
745+
example = group.example
746+
example.run group.new, reporter
747+
expect(example.reporter).to be reporter
748+
end
749+
end
735750
end

0 commit comments

Comments
 (0)