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

Commit c0f9b8a

Browse files
committed
Fix metadata backwards compat to handle deep nesting.
This is a bit confusing, but the behavior in 2.x was: * a group's `metadata` did not have it's computed keys directly available; they were exposed off of the `:example_group` subhash. * The `:example_group` subhash had a `:example_group` key that exposed the parent group's metadata (both user metadata and computed keys). Before this, we were inserting extra `:example_group` subhashes between the layers.
1 parent 7f3ace2 commit c0f9b8a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/rspec/core/metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class LegacyExampleGroupHash
370370

371371
def initialize(metadata)
372372
@metadata = metadata
373-
self[:example_group] = metadata[:parent_example_group]
373+
self[:example_group] = metadata[:parent_example_group][:example_group]
374374
end
375375

376376
def to_h

spec/rspec/core/metadata_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,29 @@ def value_for(*args)
460460
describe("nested") { child = metadata }
461461
end
462462

463-
expect(child[:example_group][:example_group]).to include(
463+
expect(child[:example_group][:example_group].to_h).to include(
464464
:foo => 3,
465465
:description => "Object group",
466466
:line_number => parent_line
467467
)
468468
end
469469

470+
it "works properly with deep nesting" do
471+
inner_metadata = nil
472+
473+
RSpec.describe "Level 1" do
474+
describe "Level 2" do
475+
describe "Level 3" do
476+
inner_metadata = metadata
477+
end
478+
end
479+
end
480+
481+
expect(inner_metadata[:example_group][:description]).to eq("Level 3")
482+
expect(inner_metadata[:example_group][:example_group][:description]).to eq("Level 2")
483+
expect(inner_metadata[:example_group][:example_group][:example_group][:description]).to eq("Level 1")
484+
end
485+
470486
it 'can mutate attributes when accessing them via [:example_group]' do
471487
meta = nil
472488

0 commit comments

Comments
 (0)