Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 4d67748

Browse files
committed
add let()
1 parent a85790b commit 4d67748

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

lib/rspec/core/example.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ def run_finished(status, results={})
5757
end
5858

5959
def run_before_each
60-
@behaviour_instance._setup_mocks if @behaviour_instance.respond_to?(:_setup_mocks)
61-
@behaviour.eval_before_eachs(@behaviour_instance)
60+
@example_group_instance._setup_mocks if @example_group_instance.respond_to?(:_setup_mocks)
61+
@behaviour.eval_before_eachs(@example_group_instance)
6262
end
6363

6464
def run_after_each
65-
@behaviour.eval_after_eachs(@behaviour_instance)
66-
@behaviour_instance._verify_mocks if @behaviour_instance.respond_to?(:_verify_mocks)
65+
@behaviour.eval_after_eachs(@example_group_instance)
66+
@example_group_instance._verify_mocks if @example_group_instance.respond_to?(:_verify_mocks)
6767
ensure
68-
@behaviour_instance._teardown_mocks if @behaviour_instance.respond_to?(:_teardown_mocks)
68+
@example_group_instance._teardown_mocks if @example_group_instance.respond_to?(:_teardown_mocks)
6969
end
7070

7171
def assign_auto_description
@@ -75,9 +75,9 @@ def assign_auto_description
7575
end
7676
end
7777

78-
def run(behaviour_instance)
79-
@behaviour_instance = behaviour_instance
80-
@behaviour_instance.running_example = self
78+
def run(example_group_instance)
79+
@example_group_instance = example_group_instance.reset
80+
@example_group_instance.running_example = self
8181

8282
run_started
8383

@@ -86,7 +86,7 @@ def run(behaviour_instance)
8686

8787
begin
8888
run_before_each
89-
@behaviour_instance.instance_eval(&example_block) if example_block
89+
@example_group_instance.instance_eval(&example_block) if example_block
9090
rescue Exception => e
9191
exception_encountered = e
9292
all_systems_nominal = false
@@ -100,7 +100,7 @@ def run(behaviour_instance)
100100
exception_encountered ||= e
101101
all_systems_nominal = false
102102
ensure
103-
@behaviour_instance.running_example = nil
103+
@example_group_instance.running_example = nil
104104
end
105105

106106
if exception_encountered

lib/rspec/core/example_group.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,23 @@ def self.eval_after_eachs(running_behaviour)
206206
end
207207

208208
def self.run(reporter)
209-
behaviour_instance = new
209+
example_group_instance = new
210210
reporter.add_behaviour(self)
211-
eval_before_alls(behaviour_instance)
212-
success = run_examples(behaviour_instance, reporter)
213-
eval_after_alls(behaviour_instance)
211+
eval_before_alls(example_group_instance)
212+
success = run_examples(example_group_instance, reporter)
213+
eval_after_alls(example_group_instance)
214214

215215
success
216216
end
217217

218218
# Runs all examples, returning true only if all of them pass
219-
def self.run_examples(behaviour_instance, reporter)
220-
examples_to_run.map { |ex| ex.run(behaviour_instance) }.all?
219+
def self.run_examples(example_group_instance, reporter)
220+
examples_to_run.map { |ex| ex.run(example_group_instance) }.all?
221+
end
222+
223+
def reset
224+
assignments.clear
225+
self
221226
end
222227

223228
def self.subclass(base_name, &body) # :nodoc:
@@ -233,7 +238,17 @@ def self.subclass(base_name, &body) # :nodoc:
233238
def self.to_s
234239
self == Rspec::Core::ExampleGroup ? 'Rspec::Core::ExampleGroup' : name
235240
end
241+
242+
def assignments
243+
@assignments ||= {}
244+
end
236245

246+
def self.let(name, &block)
247+
define_method name do
248+
assignments[name] ||= instance_eval(&block)
249+
end
250+
end
251+
237252
end
238253
end
239254
end

spec/rspec/core/example_group_spec.rb

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
def empty_behaviour_group(name='Empty ExampleGroup Group')
3+
def empty_example_group(name='Empty ExampleGroup Group')
44
group = Rspec::Core::ExampleGroup.describe(Object, name) {}
55
remove_last_describe_from_world
66
yield group if block_given?
@@ -35,7 +35,7 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
3535

3636
it "should be built correctly when nested" do
3737
behaviour_to_test = nil
38-
group = empty_behaviour_group('test')
38+
group = empty_example_group('test')
3939
group.name.should == 'Object test'
4040

4141
nested_group_one = group.describe('nested one') { }
@@ -127,13 +127,13 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
127127
describe "adding before and after hooks" do
128128

129129
it "should expose the before each blocks at before_eachs" do
130-
group = empty_behaviour_group
130+
group = empty_example_group
131131
group.before(:each) { 'foo' }
132132
group.should have(1).before_eachs
133133
end
134134

135135
it "should maintain the before each block order" do
136-
group = empty_behaviour_group
136+
group = empty_example_group
137137
group.before(:each) { 15 }
138138
group.before(:each) { 'A' }
139139
group.before(:each) { 33.5 }
@@ -144,13 +144,13 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
144144
end
145145

146146
it "should expose the before all blocks at before_alls" do
147-
group = empty_behaviour_group
147+
group = empty_example_group
148148
group.before(:all) { 'foo' }
149149
group.should have(1).before_alls
150150
end
151151

152152
it "should maintain the before all block order" do
153-
group = empty_behaviour_group
153+
group = empty_example_group
154154
group.before(:all) { 15 }
155155
group.before(:all) { 'A' }
156156
group.before(:all) { 33.5 }
@@ -161,13 +161,13 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
161161
end
162162

163163
it "should expose the after each blocks at after_eachs" do
164-
group = empty_behaviour_group
164+
group = empty_example_group
165165
group.after(:each) { 'foo' }
166166
group.should have(1).after_eachs
167167
end
168168

169169
it "should maintain the after each block order" do
170-
group = empty_behaviour_group
170+
group = empty_example_group
171171
group.after(:each) { 15 }
172172
group.after(:each) { 'A' }
173173
group.after(:each) { 33.5 }
@@ -178,13 +178,13 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
178178
end
179179

180180
it "should expose the after all blocks at after_alls" do
181-
group = empty_behaviour_group
181+
group = empty_example_group
182182
group.after(:all) { 'foo' }
183183
group.should have(1).after_alls
184184
end
185185

186186
it "should maintain the after each block order" do
187-
group = empty_behaviour_group
187+
group = empty_example_group
188188
group.after(:all) { 15 }
189189
group.after(:all) { 'A' }
190190
group.after(:all) { 33.5 }
@@ -199,21 +199,21 @@ def empty_behaviour_group(name='Empty ExampleGroup Group')
199199
describe "adding examples" do
200200

201201
it "should allow adding an example using 'it'" do
202-
group = empty_behaviour_group
202+
group = empty_example_group
203203
group.it("should do something") { }
204204
group.examples.size.should == 1
205205
end
206206

207207
it "should expose all examples at examples" do
208-
group = empty_behaviour_group
208+
group = empty_example_group
209209
group.it("should do something 1") { }
210210
group.it("should do something 2") { }
211211
group.it("should do something 3") { }
212212
group.examples.size.should == 3
213213
end
214214

215215
it "should maintain the example order" do
216-
group = empty_behaviour_group
216+
group = empty_example_group
217217
group.it("should 1") { }
218218
group.it("should 2") { }
219219
group.it("should 3") { }
@@ -350,4 +350,26 @@ def stub_behaviour
350350

351351
end
352352

353+
describe "#let" do
354+
let(:counter) do
355+
Class.new do
356+
def initialize
357+
@count = 0
358+
end
359+
def count
360+
@count += 1
361+
end
362+
end.new
363+
end
364+
365+
it "generates an instance method" do
366+
counter.count.should == 1
367+
end
368+
369+
it "caches the value" do
370+
counter.count.should == 1
371+
counter.count.should == 2
372+
end
373+
end
374+
353375
end

0 commit comments

Comments
 (0)