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

Commit aebf968

Browse files
committed
Merge pull request #2006 from gavingmiller/master
Provide a human readable format when inspecting an RSpec::Core::Example
2 parents 1ea34aa + 0fe6b1e commit aebf968

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/rspec/core/example.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def initialize(example_group_class, description, user_metadata, example_block=ni
170170
@reporter = RSpec::Core::NullReporter
171171
end
172172

173+
# Provide a human-readable representation of this class
174+
def inspect
175+
"#<#{self.class.name} #{description.inspect}>"
176+
end
177+
alias to_s inspect
178+
173179
# @return [RSpec::Core::Reporter] the current reporter for the example
174180
attr_reader :reporter
175181

@@ -256,13 +262,13 @@ class Procsy
256262
attr_reader :example
257263

258264
Example.public_instance_methods(false).each do |name|
259-
next if name.to_sym == :run || name.to_sym == :inspect
265+
next if name.to_sym == :run || name.to_sym == :inspect || name.to_sym == :to_s
260266

261267
define_method(name) { |*a, &b| @example.__send__(name, *a, &b) }
262268
end
263269

264270
Proc.public_instance_methods(false).each do |name|
265-
next if name.to_sym == :call || name.to_sym == :inspect || name.to_sym == :to_proc
271+
next if name.to_sym == :call || name.to_sym == :inspect || name.to_sym == :to_s || name.to_sym == :to_proc
266272

267273
define_method(name) { |*a, &b| @proc.__send__(name, *a, &b) }
268274
end

spec/rspec/core/example_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def metadata_hash(*args)
2222
expect { ignoring_warnings { pp example_instance }}.to output(/RSpec::Core::Example/).to_stdout
2323
end
2424

25+
describe "human readable output" do
26+
it 'prints a human readable description when inspected' do
27+
expect(example_instance.inspect).to eq("#<RSpec::Core::Example \"example description\">")
28+
end
29+
30+
it 'prints a human readable description for #to_s' do
31+
expect(example_instance.to_s).to eq("#<RSpec::Core::Example \"example description\">")
32+
end
33+
end
34+
2535
describe "#rerun_argument" do
2636
it "returns the location-based rerun argument" do
2737
allow(RSpec.configuration).to receive_messages(:loaded_spec_files => [__FILE__])

0 commit comments

Comments
 (0)