Navigation Menu

Skip to content

Commit

Permalink
Fix issue when RSpec invokes the subject block multiple times
Browse files Browse the repository at this point in the history
- Closes #244.
  • Loading branch information
lackac authored and dchelimsky committed Dec 6, 2010
1 parent 349b7ec commit 31db524
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions features/subject/explicit_subject.feature
Expand Up @@ -60,3 +60,19 @@ Feature: explicit subject
"""
When I run "rspec helper_subject_spec.rb"
Then the output should contain "1 example, 0 failures"

Scenario: invoke subject block at most once per example
Given a file named "nil_subject_spec.rb" with:
"""
describe Array do
describe "[]" do
context "with index out of bounds" do
before { Array.should_receive(:one_two_three).once.and_return([1,2,3]) }
subject { Array.one_two_three[42] }
it { should be_nil }
end
end
end
"""
When I run "rspec nil_subject_spec.rb"
Then the output should contain "1 example, 0 failures"
8 changes: 6 additions & 2 deletions lib/rspec/core/subject.rb
Expand Up @@ -28,7 +28,11 @@ module InstanceMethods
# end
# end
def subject
@original_subject ||= instance_eval(&self.class.subject)
if defined?(@original_subject)
@original_subject
else
@original_subject = instance_eval(&self.class.subject)
end
end

begin
Expand Down Expand Up @@ -86,7 +90,7 @@ module ClassMethods
# The attribute can be a +Symbol+ or a +String+. Given a +String+
# with dots, the result is as though you concatenated that +String+
# onto the subject in an expression.
#
#
# describe Person do
# subject do
# Person.new.tap do |person|
Expand Down

0 comments on commit 31db524

Please sign in to comment.