Skip to content
Browse files

Fix issue when RSpec invokes the subject block multiple times

  • Loading branch information...
1 parent 8686ecc commit 16ae8981d8ffe47ae529a1383a2cb20e62bfdca4 @lackac committed Dec 5, 2010
Showing with 22 additions and 2 deletions.
  1. +16 −0 features/subject/explicit_subject.feature
  2. +6 −2 lib/rspec/core/subject.rb
View
16 features/subject/explicit_subject.feature
@@ -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"
View
8 lib/rspec/core/subject.rb
@@ -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
@@ -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|

0 comments on commit 16ae898

Please sign in to comment.
Something went wrong with that request. Please try again.