Permalink
Browse files

Merge pull request #582 from masarakki/master

Support false and nil subject
  • Loading branch information...
2 parents b93433d + 54c0e18 commit 59c5dde12e5169ceedb9970908ccff00c5cc64f7 @dchelimsky dchelimsky committed Mar 17, 2012
Showing with 22 additions and 1 deletion.
  1. +4 −1 lib/rspec/core/subject.rb
  2. +18 −0 spec/rspec/core/subject_spec.rb
View
5 lib/rspec/core/subject.rb
@@ -120,13 +120,16 @@ def its(attribute, &block)
example do
self.class.class_eval do
define_method(:subject) do
- @_subject ||= if attribute.is_a?(Array)
+ unless instance_variable_defined?(:@_subject)
+ @_subject = if attribute.is_a?(Array)
super()[*attribute]
else
attribute.to_s.split('.').inject(super()) do |target, method|
target.send(method)
end
end
+ end
+ @_subject
end
end
instance_eval(&block)
View
18 spec/rspec/core/subject_spec.rb
@@ -196,6 +196,24 @@ def subject; super().first; end
end
end
+ context "memorize subject" do
+ subject do
+ Class.new do
+ def initialize
+ @counter = 0
+ end
+ def false_if_first_time
+ if @counter == 0
+ @counter += 1
+ false
+ else
+ true
+ end
+ end
+ end.new
+ end
+ its(:false_if_first_time) { should be_false }
+ end
end
end
end

0 comments on commit 59c5dde

Please sign in to comment.