Permalink
Browse files

Named subject can be referred from an inside subject block

Example:

    describe "list" do
      subject(:list) { [1,2,3] }
      describe 'first' do
        subject(:first_element) { list.first }
        it { should eq(1) }
      end
    end

With the previous implementation, this fails with "stack level too deep".
This error is not obvious and irritating to me.

I believe this behaviour is more natural.
  • Loading branch information...
tomykaira committed Sep 1, 2012
1 parent f1ef071 commit c83eaf9b7ad3400dec80ebccbdb9c646640e6899
Showing with 17 additions and 2 deletions.
  1. +6 −2 lib/rspec/core/subject.rb
  2. +11 −0 spec/rspec/core/subject_spec.rb
@@ -193,8 +193,12 @@ def its(attribute, &block)
# @see ExampleMethods#subject
# @see ExampleMethods#should
def subject(name=nil, &block)
- define_method(name) { subject } if name
- block ? @explicit_subject_block = block : explicit_subject || implicit_subject
+ if name
+ let(name, &block)
+ subject { instance_eval(name.to_s) }
+ else
+ block ? @explicit_subject_block = block : explicit_subject || implicit_subject
+ end
end
attr_reader :explicit_subject_block
@@ -91,6 +91,17 @@ module RSpec::Core
end
group.run.should be_true
end
+
+ it "is referred from inside subject by the name" do
+ group = ExampleGroup.describe do
+ subject(:list) { [1,2,3] }
+ describe 'first' do
+ subject(:first_element) { list.first }
+ it { should eq(1) }
+ end
+ end
+ group.run.should be_true
+ end
end
end

0 comments on commit c83eaf9

Please sign in to comment.