/
example_group_spec.rb
425 lines (342 loc) · 12.5 KB
/
example_group_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
require 'spec_helper'
class SelfObserver
def self.cache
@cache ||= []
end
def initialize
self.class.cache << self
end
end
module Rspec::Core
describe ExampleGroup do
describe "top level group" do
it "runs its children" do
examples_run = []
parent = ExampleGroup.describe("parent")
child = parent.describe("child") do
it "does something" do
examples_run << running_example
end
end
child.filtered_examples.replace(child.examples)
parent.run_all
examples_run.should have(1).example
end
describe "descendents" do
it "returns self + all descendents" do
group = ExampleGroup.describe("parent") do
describe("child") do
describe("grandchild 1") {}
describe("grandchild 2") {}
end
end
group.descendents.size.should == 4
end
end
end
describe "child" do
it "is known by parent" do
parent = ExampleGroup.describe
child = parent.describe
parent.children.should == [child]
end
it "is not registered in world" do
parent = ExampleGroup.describe
child = parent.describe
Rspec.world.example_groups.should == [parent]
end
end
describe "filtering" do
it "includes all examples in an explicitly included group" do
Rspec.world.stub(:inclusion_filter).and_return({ :awesome => true })
group = ExampleGroup.describe("does something", :awesome => true)
examples = [
group.example("first"),
group.example("second")
]
group.filtered_examples.should == examples
end
it "includes explicitly included examples" do
Rspec.world.stub(:inclusion_filter).and_return({ :awesome => true })
group = ExampleGroup.describe
example = group.example("does something", :awesome => true)
group.example("don't run me")
group.filtered_examples.should == [example]
end
it "excludes all examples in an excluded group" do
Rspec.world.stub(:exclusion_filter).and_return({ :awesome => false })
group = ExampleGroup.describe("does something", :awesome => false)
examples = [
group.example("first"),
group.example("second")
]
group.filtered_examples.should == []
end
it "filters out excluded examples" do
Rspec.world.stub(:exclusion_filter).and_return({ :awesome => false })
group = ExampleGroup.describe("does something")
examples = [
group.example("first", :awesome => false),
group.example("second")
]
group.filtered_examples.should == [examples[1]]
end
context "with no filters" do
it "returns all" do
group = ExampleGroup.describe
example = group.example("does something")
group.filtered_examples.should == [example]
end
end
context "with no examples or groups that match filters" do
it "returns none" do
Rspec.world.stub(:inclusion_filter).and_return({ :awesome => false })
group = ExampleGroup.describe
example = group.example("does something")
group.filtered_examples.should == []
end
end
end
describe '#describes' do
context "with a constant as the first parameter" do
it "is that constant" do
ExampleGroup.describe(Object) { }.describes.should == Object
end
end
context "with a string as the first parameter" do
it "is nil" do
ExampleGroup.describe("i'm a computer") { }.describes.should be_nil
end
end
end
describe '#description' do
it "grabs the description from the metadata" do
group = ExampleGroup.describe(Object, "my desc") { }
group.description.should == group.metadata[:example_group][:description]
end
end
describe '#metadata' do
it "adds the third parameter to the metadata" do
ExampleGroup.describe(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
end
it "adds the caller to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:caller].any? {|f|
f =~ /#{__FILE__}/
}.should be_true
end
it "adds the the file_path to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:file_path].should == __FILE__
end
it "has a reader for file_path" do
ExampleGroup.describe(Object) { }.file_path.should == __FILE__
end
it "adds the line_number to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:line_number].should == __LINE__
end
end
describe "before, after, and around hooks" do
it "runs the before alls in order" do
group = ExampleGroup.describe
order = []
group.before(:all) { order << 1 }
group.before(:all) { order << 2 }
group.before(:all) { order << 3 }
group.example("example") {}
group.run_all
order.should == [1,2,3]
end
it "runs the before eachs in order" do
group = ExampleGroup.describe
order = []
group.before(:each) { order << 1 }
group.before(:each) { order << 2 }
group.before(:each) { order << 3 }
group.example("example") {}
group.run_all
order.should == [1,2,3]
end
it "runs the after eachs in reverse order" do
group = ExampleGroup.describe
order = []
group.after(:each) { order << 1 }
group.after(:each) { order << 2 }
group.after(:each) { order << 3 }
group.example("example") {}
group.run_all
order.should == [3,2,1]
end
it "runs the after alls in reverse order" do
group = ExampleGroup.describe
order = []
group.after(:all) { order << 1 }
group.after(:all) { order << 2 }
group.after(:all) { order << 3 }
group.example("example") {}
group.run_all
order.should == [3,2,1]
end
it "runs before all, before each, example, after each, after all, in that order" do
group = ExampleGroup.describe
order = []
group.after(:all) { order << :after_all }
group.after(:each) { order << :after_each }
group.before(:each) { order << :before_each }
group.before(:all) { order << :before_all }
group.example("example") { order << :example }
group.run_all
order.should == [
:before_all,
:before_each,
:example,
:after_each,
:after_all
]
end
it "exposes the around each blocks at after_alls" do
group = ExampleGroup.describe
group.around(:each) { 'foo' }
group.should have(1).around_eachs
end
end
describe "adding examples" do
it "allows adding an example using 'it'" do
group = ExampleGroup.describe
group.it("should do something") { }
group.examples.size.should == 1
end
it "exposes all examples at examples" do
group = ExampleGroup.describe
group.it("should do something 1") { }
group.it("should do something 2") { }
group.it("should do something 3") { }
group.should have(3).examples
end
it "maintains the example order" do
group = ExampleGroup.describe
group.it("should 1") { }
group.it("should 2") { }
group.it("should 3") { }
group.examples[0].description.should == 'should 1'
group.examples[1].description.should == 'should 2'
group.examples[2].description.should == 'should 3'
end
end
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
describe "A sample nested group", :nested_describe => "yep" do
it "sets the described class to the constant Object" do
running_example.example_group.describes.should == Object
end
it "sets the description to 'A sample nested describe'" do
running_example.example_group.description.should == 'A sample nested group'
end
it "has top level metadata from the example_group and its ancestors" do
running_example.example_group.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
it "exposes the parent metadata to the contained examples" do
running_example.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
end
end
describe "#run_examples" do
let(:reporter) { double("reporter").as_null_object }
it "returns true if all examples pass" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 1 }
example('ex 2') { 1.should == 1 }
end
group.stub(:filtered_examples) { group.examples }
group.run(reporter).should be_true
end
it "returns false if any of the examples fail" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 1 }
example('ex 2') { 1.should == 2 }
end
group.stub(:filtered_examples) { group.examples }
group.run(reporter).should be_false
end
it "runs all examples, regardless of any of them failing" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should == 2 }
example('ex 2') { 1.should == 1 }
end
group.stub(:filtered_examples) { group.examples }
group.filtered_examples.each do |example|
example.should_receive(:run)
end
group.run(reporter).should be_false
end
end
describe "how instance variables are inherited" do
before(:all) do
@before_all_top_level = 'before_all_top_level'
end
before(:each) do
@before_each_top_level = 'before_each_top_level'
end
it "should be able to access a before each ivar at the same level" do
@before_each_top_level.should == 'before_each_top_level'
end
it "should be able to access a before all ivar at the same level" do
@before_all_top_level.should == 'before_all_top_level'
end
it "should be able to access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do
running_example.example_group.before_all_ivars.should include('@before_all_top_level' => 'before_all_top_level')
end
it "should be able to access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do
running_example.example_group.before_all_ivars.should include(:@before_all_top_level => 'before_all_top_level')
end
describe "but now I am nested" do
it "should be able to access a parent example groups before each ivar at a nested level" do
@before_each_top_level.should == 'before_each_top_level'
end
it "should be able to access a parent example groups before all ivar at a nested level" do
@before_all_top_level.should == "before_all_top_level"
end
it "changes to before all ivars from within an example do not persist outside the current describe" do
@before_all_top_level = "ive been changed"
end
describe "accessing a before_all ivar that was changed in a parent example_group" do
it "does not have access to the modified version" do
@before_all_top_level.should == 'before_all_top_level'
end
end
end
end
describe "ivars are not shared across examples" do
it "(first example)" do
@a = 1
@b.should be_nil
end
it "(second example)" do
@b = 2
@a.should be_nil
end
end
describe "#around" do
around(:each) do |example|
SelfObserver.new
example.run
SelfObserver.cache.clear
end
it "has 1 SelfObserver (1)" do
SelfObserver.cache.length.should == 1
end
it "has 1 SelfObserver (2)" do
SelfObserver.cache.length.should == 1
end
end
describe "top_level_description" do
it "returns the description from the outermost example group" do
group = nil
ExampleGroup.describe("top") do
context "middle" do
group = describe "bottom" do
end
end
end
group.top_level_description.should == "top"
end
end
end
end