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

Commit b39385e

Browse files
committed
Make described_class to return innermost described class.
It used to return the outermost described class. It doesn't happen very often that there is more than one class being described in the set of nested example groups (and we generally recommend against it), but this was surprising behavior to me. Fixes #1114.
1 parent e9aa71e commit b39385e

File tree

5 files changed

+39
-27
lines changed

5 files changed

+39
-27
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
### 3.0.0.rc1 Development
22
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.0.0.beta2...master)
33

4+
Breaking Changes for 3.0.0:
5+
6+
* Change `described_class` so that in a nested group like `describe
7+
MyClass`, it returns `MyClass` rather than the outer group's described
8+
class. (Myron Marston)
9+
410
Enhancements:
511

612
* Add `config.default_formatter` attribute, which can be used to set a

features/subject/implicit_subject.feature

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Feature: implicitly defined subject
3333
When I run `rspec nested_subject_spec.rb`
3434
Then the examples should all pass
3535

36-
Scenario: subject in a nested group with a different class (outermost wins)
36+
Scenario: subject in a nested group with a different class (innermost wins)
3737
Given a file named "nested_subject_spec.rb" with:
3838
"""ruby
3939
class ArrayWithOneElement < Array
@@ -46,14 +46,8 @@ Feature: implicitly defined subject
4646
describe Array do
4747
describe ArrayWithOneElement do
4848
context "referenced as subject" do
49-
it "should be empty (because it is the Array declared at the top)" do
50-
expect(subject).to be_empty
51-
end
52-
end
53-
54-
context "created in the example" do
55-
it "should not be empty" do
56-
expect(ArrayWithOneElement.new).not_to be_empty
49+
it "contains one element" do
50+
expect(subject).to include("first element")
5751
end
5852
end
5953
end

lib/rspec/core/metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def described_class
150150
end
151151
end
152152

153-
container_stack.reverse.each do |g|
153+
container_stack.each do |g|
154154
candidate = g[:description_args].first
155155
return candidate unless String === candidate || Symbol === candidate
156156
end

spec/rspec/core/example_group_spec.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def ascending_numbers
472472
context "in a nested group" do
473473
it "inherits the described class/module from the outer group" do
474474
group = ExampleGroup.describe(String) do
475-
describe Array do
475+
describe "nested" do
476476
example "describes is String" do
477477
expect(described_class).to eq(String)
478478
end
@@ -481,6 +481,18 @@ def ascending_numbers
481481

482482
expect(group.run).to be_truthy, "expected examples in group to pass"
483483
end
484+
485+
it "overrides the described class when a class is passed" do
486+
value = nil
487+
488+
ExampleGroup.describe(String) do
489+
describe Array do
490+
example { value = described_class }
491+
end
492+
end.run
493+
494+
expect(value).to eq(Array)
495+
end
484496
end
485497

486498
context "for `describe(SomeClass)` within a `describe 'some string' group" do
@@ -1111,8 +1123,8 @@ def extract_execution_results(group)
11111123
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
11121124

11131125
describe "A sample nested group", :nested_describe => "yep" do
1114-
it "sets the described class to the described class of the outer most group" do |ex|
1115-
expect(ex.example_group.described_class).to eq(ExampleGroup)
1126+
it "sets the described class to the nearest described class" do |ex|
1127+
expect(ex.example_group.described_class).to eq(Object)
11161128
end
11171129

11181130
it "sets the description to 'A sample nested describe'" do |ex|

spec/rspec/core/metadata_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,32 +290,32 @@ module Core
290290
end
291291

292292
context "in a nested group" do
293-
it "returns the parent group's described class" do
294-
sm = Metadata.new
295-
sm.process(String)
293+
it "inherits the parent group's described class" do
294+
parent = Metadata.new
295+
parent.process(Hash)
296296

297-
m = Metadata.new(sm)
298-
m.process(Array)
297+
child = Metadata.new(parent)
298+
child.process("sub context")
299299

300-
expect(m[:example_group][key]).to be(String)
300+
expect(child[:example_group][key]).to be(Hash)
301301
end
302302

303-
it "returns own described class if parent doesn't have one" do
304-
sm = Metadata.new
305-
sm.process("foo")
303+
it "sets the described class when passing a class" do
304+
parent = Metadata.new
305+
parent.process(String)
306306

307-
m = Metadata.new(sm)
308-
m.process(Array)
307+
child = Metadata.new(parent)
308+
child.process(Array)
309309

310-
expect(m[:example_group][key]).to be(Array)
310+
expect(child[:example_group][key]).to be(Array)
311311
end
312312

313-
it "can override a parent group's described class" do
313+
it "can override a parent group's described class using metdata" do
314314
parent = Metadata.new
315315
parent.process(String)
316316

317317
child = Metadata.new(parent)
318-
child.process(Fixnum)
318+
child.process("sub context")
319319
child[:example_group][key] = Hash
320320

321321
grandchild = Metadata.new(child)

0 commit comments

Comments
 (0)