New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add better inspect
output for ExampleGroup
#1687
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1604,5 +1604,97 @@ def foo; end | |
end | ||
}.to raise_error(/not allowed/) | ||
end | ||
|
||
describe 'inspect output' do | ||
context 'when there is no inspect output provided' do | ||
it "uses '(no description provided)' instead" do | ||
expect(ExampleGroup.new.inspect).to eq('#<RSpec::Core::ExampleGroup (no description provided)>') | ||
end | ||
end | ||
|
||
context 'when an example has a description' do | ||
it 'includes description and location' do | ||
an_example = nil | ||
|
||
line = __LINE__ + 2 | ||
group = ExampleGroup.describe 'SomeClass1' do | ||
example 'an example' do | ||
an_example = self | ||
end | ||
end | ||
|
||
group.run | ||
|
||
path = RSpec::Core::Metadata.relative_path(__FILE__) | ||
expect(an_example.inspect).to eq("#<RSpec::ExampleGroups::SomeClass1 \"an example\" (#{path}:#{line})>") | ||
end | ||
end | ||
|
||
context 'when an example does not have a description' do | ||
it 'includes fallback description' do | ||
an_example = nil | ||
|
||
line = __LINE__ + 2 | ||
group = ExampleGroup.describe 'SomeClass2' do | ||
example do | ||
an_example = self | ||
end | ||
end | ||
|
||
group.run | ||
|
||
path = RSpec::Core::Metadata.relative_path(__FILE__) | ||
expect(an_example.inspect).to eq("#<RSpec::ExampleGroups::SomeClass2 \"example at #{path}:#{line}\">") | ||
end | ||
end | ||
|
||
it 'handles before context hooks' do | ||
a_before_hook = nil | ||
|
||
group = ExampleGroup.describe 'SomeClass3' do | ||
before(:context) do | ||
a_before_hook = self | ||
end | ||
|
||
example {} | ||
end | ||
|
||
group.run | ||
expect(a_before_hook.inspect).to eq("#<RSpec::ExampleGroups::SomeClass3 before(:context) hook>") | ||
end | ||
|
||
it 'handles after context hooks' do | ||
an_after_hook = nil | ||
|
||
group = ExampleGroup.describe 'SomeClass4' do | ||
after(:context) do | ||
an_after_hook = self | ||
end | ||
|
||
example {} | ||
end | ||
|
||
group.run | ||
expect(an_after_hook.inspect).to eq("#<RSpec::ExampleGroups::SomeClass4 after(:context) hook>") | ||
end | ||
|
||
it "does not pollute an example's `inspect` output with the inspect ivar from `before(:context)`" do | ||
inspect_output = nil | ||
|
||
line = __LINE__ + 2 | ||
group = ExampleGroup.describe do | ||
example do | ||
inspect_output = inspect | ||
end | ||
|
||
before(:context) {} | ||
end | ||
|
||
group.run | ||
|
||
path = RSpec::Core::Metadata.relative_path(__FILE__) | ||
expect(inspect_output).to end_with("\"example at #{path}:#{line}\">") | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing the instance variable directly like this is a code smell, IMO. I think this spec can be tweaked slightly to retain it's essence without doing that: it 'does not clear the inspect output after the example runs' do
an_example = nil
line = __LINE__ + 2
group = ExampleGroup.describe do
example do
an_example = self
end
before(:context) {}
end
group.run
path = RSpec::Core::Metadata.relative_path(__FILE__)
expect(an_example.inspect).to end_with("\"example at #{path}:#{line}\">")
end Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm going to get rid of this spec. It's very similar to: it 'includes description and location' do
an_example = nil
line = __LINE__ + 2
group = ExampleGroup.describe 'SomeClass1' do
example 'an example' do
an_example = self
end
end
group.run
path = RSpec::Core::Metadata.relative_path(__FILE__)
expect(an_example.inspect).to eq("#<RSpec::ExampleGroups::SomeClass1 \"an example\" (#{path}:#{line})>")
end |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ruby version differences we tend to prefer to define differing versions of methods to make it explicit what environments need which logic and to only incur the overhead at file load time rather than each time the method is called. In this case, it could be: