Permalink
Browse files

memoize subject with its\n\n- Closes #292

  • Loading branch information...
1 parent 56b6a65 commit 599c0c9878adf7818f2a29e1b7bc5cb53a5348ca @dchelimsky dchelimsky committed Jan 27, 2011
Showing with 10 additions and 8 deletions.
  1. +7 −7 lib/rspec/core/subject.rb
  2. +3 −1 spec/rspec/core/example_group_spec.rb
@@ -124,13 +124,13 @@ def its(attribute, &block)
example do
self.class.class_eval do
define_method(:subject) do
- if super().is_a?(Hash) && attribute.is_a?(Array)
- OpenStruct.new(super()).send(attribute.first)
- else
- attribute.to_s.split('.').inject(super()) do |target, method|
- target.send(method)
- end
- end
+ @_subject ||= if super().is_a?(Hash) && attribute.is_a?(Array)
+ OpenStruct.new(super()).send(attribute.first)
+ else
+ attribute.to_s.split('.').inject(super()) do |target, method|
+ target.send(method)
+ end
+ end
end
end
instance_eval(&block)
@@ -646,7 +646,9 @@ def attribute_call_count
end
end.new
end
- its(:attribute_call_count) { should_not == 2 }
+
+ its(:attribute_call_count) { should eq(1) }
+
context "with nil value" do
subject do
Class.new do

0 comments on commit 599c0c9

Please sign in to comment.