Skip to content

Commit

Permalink
memoize subject with its\n\n- Closes rspec#292
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jan 27, 2011
1 parent 56b6a65 commit 599c0c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/rspec/core/subject.rb
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion spec/rspec/core/example_group_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 599c0c9

Please sign in to comment.