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

Commit 83f9936

Browse files
committed
Refine described_class inference further.
In the last commit, I made inner `describe SomeClass` example groups get `SomeClass` as the described_class. The logic confusingly allowed a `:described_class` metadata key in an outer group take precedence, which meant that the behavior depended on whether or not `described_class` was referenced from the outer group. This fixes that, making it work consistently: - Use `:described_class` is present. - Otherwise, use the group's `describe` arg (unless it's something like a string). - If neither of those, look in the parent and recurse.
1 parent b39385e commit 83f9936

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

lib/rspec/core/metadata.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ def described_class
148148
return value unless value.nil?
149149
end
150150
end
151-
end
152151

153-
container_stack.each do |g|
154152
candidate = g[:description_args].first
155153
return candidate unless String === candidate || Symbol === candidate
156154
end

spec/rspec/core/example_group_spec.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,16 +482,27 @@ def ascending_numbers
482482
expect(group.run).to be_truthy, "expected examples in group to pass"
483483
end
484484

485-
it "overrides the described class when a class is passed" do
486-
value = nil
485+
context "when a class is passed" do
486+
def described_class_value
487+
value = nil
488+
489+
ExampleGroup.describe(String) do
490+
yield if block_given?
491+
describe Array do
492+
example { value = described_class }
493+
end
494+
end.run
487495

488-
ExampleGroup.describe(String) do
489-
describe Array do
490-
example { value = described_class }
491-
end
492-
end.run
496+
value
497+
end
498+
499+
it "overrides the described class" do
500+
expect(described_class_value).to eq(Array)
501+
end
493502

494-
expect(value).to eq(Array)
503+
it "overrides the described class even when described_class is referenced in the outer group" do
504+
expect(described_class_value { described_class }).to eq(Array)
505+
end
495506
end
496507
end
497508

spec/rspec/core/metadata_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ module Core
319319
child[:example_group][key] = Hash
320320

321321
grandchild = Metadata.new(child)
322-
grandchild.process(Array)
322+
grandchild.process("sub context")
323323

324324
expect(grandchild[:example_group][key]).to be(Hash)
325325
expect(child[:example_group][key]).to be(Hash)

0 commit comments

Comments
 (0)