Permalink
Browse files

Allow let to memoize similar methods in a single block by passing mul…

…tple arguments
  • Loading branch information...
p8 committed Feb 16, 2012
1 parent aefb25c commit e926d9ee776b722908822dee63eba38a99fcdbdc
Showing with 21 additions and 8 deletions.
  1. +10 −6 lib/rspec/core/let.rb
  2. +11 −2 spec/rspec/core/let_spec.rb
View
@@ -19,9 +19,11 @@ module ExampleGroupMethods
# thing.should be_something
# end
# end
- def let(name, &block)
- define_method(name) do
- __memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block) }
+ def let(*names, &block)
+ names.each do |name|
+ define_method(name) do
+ __memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block) }
+ end
end
end
@@ -79,9 +81,11 @@ def let(name, &block)
# end
# end
# end
- def let!(name, &block)
- let(name, &block)
- before { __send__(name) }
+ def let!(*names, &block)
+ names.each do |name|
+ let(name, &block)
+ before { __send__(name) }
+ end
end
end
@@ -1,7 +1,7 @@
require 'spec_helper'
describe "#let" do
- let(:counter) do
+ let(:counter, :another_counter) do
Class.new do
def initialize
@count = 0
@@ -33,10 +33,14 @@ def count
@nil_value_count.should eq(1)
end
+
+ it "caches multiple arguments separately" do
+ counter.should_not eq(another_counter)
+ end
end
describe "#let!" do
- let!(:creator) do
+ let!(:creator, :another_creator) do
Class.new do
@count = 0
def self.count
@@ -52,4 +56,9 @@ def self.count
it "does not interfere between tests" do
creator.count.should eq(1)
end
+
+ it "caches multiple arguments separately" do
+ creator.should_not eq(another_creator)
+ end
+
end

0 comments on commit e926d9e

Please sign in to comment.