forked from rspec/rspec-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
before_and_after_hooks.feature
465 lines (388 loc) · 12.2 KB
/
before_and_after_hooks.feature
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
Feature: `before` and `after` hooks
Use `before` and `after` hooks to execute arbitrary code before and/or after
the body of an example is run:
```ruby
before(:example) # run before each example
before(:context) # run one time only, before all of the examples in a group
after(:example) # run after each example
after(:context) # run one time only, after all of the examples in a group
```
`before` and `after` blocks are called in the following order:
```ruby
before :suite
before :context
before :example
after :example
after :context
after :suite
```
`before` and `after` hooks can be defined directly in the example groups they
should run in, or in a global `RSpec.configure` block.
**WARNING:** Setting instance variables are not supported in `before(:suite)`.
**WARNING:** Mocks are only supported in `before(:example)`.
**WARNING:** `around` hooks will execute *after* any `before` hooks, and *before*
any `after` hooks regardless of the context they were defined in.
Note: the `:example` and `:context` scopes are also available as `:each` and
`:all`, respectively. Use whichever you prefer.
Scenario: Define `before(:example)` block
Given a file named "before_example_spec.rb" with:
"""ruby
require "rspec/expectations"
class Thing
def widgets
@widgets ||= []
end
end
RSpec.describe Thing do
before(:example) do
@thing = Thing.new
end
describe "initialized in before(:example)" do
it "has 0 widgets" do
expect(@thing.widgets.count).to eq(0)
end
it "can accept new widgets" do
@thing.widgets << Object.new
end
it "does not share state across examples" do
expect(@thing.widgets.count).to eq(0)
end
end
end
"""
When I run `rspec before_example_spec.rb`
Then the examples should all pass
Scenario: Define `before(:context)` block in example group
Given a file named "before_context_spec.rb" with:
"""ruby
require "rspec/expectations"
class Thing
def widgets
@widgets ||= []
end
end
RSpec.describe Thing do
before(:context) do
@thing = Thing.new
end
describe "initialized in before(:context)" do
it "has 0 widgets" do
expect(@thing.widgets.count).to eq(0)
end
it "can accept new widgets" do
@thing.widgets << Object.new
end
it "shares state across examples" do
expect(@thing.widgets.count).to eq(1)
end
end
end
"""
When I run `rspec before_context_spec.rb`
Then the examples should all pass
When I run `rspec before_context_spec.rb:15`
Then the examples should all pass
Scenario: Failure in `before(:context)` block
Given a file named "before_context_spec.rb" with:
"""ruby
RSpec.describe "an error in before(:context)" do
before(:context) do
raise "oops"
end
it "fails this example" do
end
it "fails this example, too" do
end
after(:context) do
puts "after context ran"
end
describe "nested group" do
it "fails this third example" do
end
it "fails this fourth example" do
end
describe "yet another level deep" do
it "fails this last example" do
end
end
end
end
"""
When I run `rspec before_context_spec.rb --format documentation`
Then the output should contain "5 examples, 5 failures"
And the output should contain:
"""
an error in before(:context)
fails this example (FAILED - 1)
fails this example, too (FAILED - 2)
nested group
fails this third example (FAILED - 3)
fails this fourth example (FAILED - 4)
yet another level deep
fails this last example (FAILED - 5)
after context ran
"""
When I run `rspec before_context_spec.rb:9 --format documentation`
Then the output should contain "1 example, 1 failure"
And the output should contain:
"""
an error in before(:context)
fails this example, too (FAILED - 1)
"""
Scenario: Failure in `after(:context)` block
Given a file named "after_context_spec.rb" with:
"""ruby
RSpec.describe "an error in after(:context)" do
after(:context) do
raise StandardError.new("Boom!")
end
it "passes this example" do
end
it "passes this example, too" do
end
end
"""
When I run `rspec after_context_spec.rb`
Then it should fail with:
"""
An error occurred in an `after(:context)` hook.
Failure/Error: raise StandardError.new("Boom!")
StandardError:
Boom!
# ./after_context_spec.rb:3
"""
Scenario: Define `before` and `after` blocks in configuration
Given a file named "befores_in_configuration_spec.rb" with:
"""ruby
require "rspec/expectations"
RSpec.configure do |config|
config.before(:example) do
@before_example = "before example"
end
config.before(:context) do
@before_context = "before context"
end
end
RSpec.describe "stuff in before blocks" do
describe "with :context" do
it "should be available in the example" do
expect(@before_context).to eq("before context")
end
end
describe "with :example" do
it "should be available in the example" do
expect(@before_example).to eq("before example")
end
end
end
"""
When I run `rspec befores_in_configuration_spec.rb`
Then the examples should all pass
Scenario: `before`/`after` blocks are run in order
Given a file named "ensure_block_order_spec.rb" with:
"""ruby
require "rspec/expectations"
RSpec.describe "before and after callbacks" do
before(:context) do
puts "before context"
end
before(:example) do
puts "before example"
end
after(:example) do
puts "after example"
end
after(:context) do
puts "after context"
end
it "gets run in order" do
end
end
"""
When I run `rspec --format progress ensure_block_order_spec.rb`
Then the output should contain:
"""
before context
before example
after example
.after context
"""
Scenario: `before`/after` blocks defined in configuration are run in order
Given a file named "configuration_spec.rb" with:
"""ruby
require "rspec/expectations"
RSpec.configure do |config|
config.before(:suite) do
puts "before suite"
end
config.before(:context) do
puts "before context"
end
config.before(:example) do
puts "before example"
end
config.after(:example) do
puts "after example"
end
config.after(:context) do
puts "after context"
end
config.after(:suite) do
puts "after suite"
end
end
RSpec.describe "ignore" do
example "ignore" do
end
end
"""
When I run `rspec --format progress configuration_spec.rb`
Then the output should contain:
"""
before suite
before context
before example
after example
.after context
after suite
"""
Scenario: `after` blocks are run also for examples which have failed or encountered other errors
Given a file named "after_example_spec.rb" with:
"""ruby
RSpec.describe "after callbacks" do
after(:example) do
puts "this is executed"
raise "error in after example block"
end
after(:example) do
puts "this is also executed"
raise "another error"
end
example "deliberate failure" do
expect(true).to be(false)
end
end
"""
When I run `rspec --format progress after_example_spec.rb`
Then the output should contain "1 example, 1 failure"
And the output should contain "Got 1 failure and 2 other errors"
And the output should contain "this is executed"
And the output should contain "this is also executed"
Scenario: `before`/`after` context blocks are run once
Given a file named "before_and_after_context_spec.rb" with:
"""ruby
RSpec.describe "before and after callbacks" do
before(:context) do
puts "outer before context"
end
example "in outer group" do
end
after(:context) do
puts "outer after context"
end
describe "nested group" do
before(:context) do
puts "inner before context"
end
example "in nested group" do
end
after(:context) do
puts "inner after context"
end
end
end
"""
When I run `rspec --format progress before_and_after_context_spec.rb`
Then the examples should all pass
And the output should contain:
"""
outer before context
.inner before context
.inner after context
outer after context
"""
When I run `rspec --format progress before_and_after_context_spec.rb:14`
Then the examples should all pass
And the output should contain:
"""
outer before context
inner before context
.inner after context
outer after context
"""
When I run `rspec --format progress before_and_after_context_spec.rb:6`
Then the examples should all pass
And the output should contain:
"""
outer before context
.outer after context
"""
Scenario: Nested examples have access to state set in outer `before(:context)`
Given a file named "before_context_spec.rb" with:
"""ruby
RSpec.describe "something" do
before :context do
@value = 123
end
describe "nested" do
it "access state set in before(:context)" do
expect(@value).to eq(123)
end
describe "nested more deeply" do
it "access state set in before(:context)" do
expect(@value).to eq(123)
end
end
end
describe "nested in parallel" do
it "access state set in before(:context)" do
expect(@value).to eq(123)
end
end
end
"""
When I run `rspec before_context_spec.rb`
Then the examples should all pass
Scenario: `before`/`after` context blocks have access to state
Given a file named "before_and_after_context_spec.rb" with:
"""ruby
RSpec.describe "before and after callbacks" do
before(:context) do
@outer_state = "set in outer before context"
end
example "in outer group" do
expect(@outer_state).to eq("set in outer before context")
end
describe "nested group" do
before(:context) do
@inner_state = "set in inner before context"
end
example "in nested group" do
expect(@outer_state).to eq("set in outer before context")
expect(@inner_state).to eq("set in inner before context")
end
after(:context) do
expect(@inner_state).to eq("set in inner before context")
end
end
after(:context) do
expect(@outer_state).to eq("set in outer before context")
end
end
"""
When I run `rspec before_and_after_context_spec.rb`
Then the examples should all pass
Scenario: Exception in `before(:example)` is captured and reported as failure
Given a file named "error_in_before_example_spec.rb" with:
"""ruby
RSpec.describe "error in before(:example)" do
before(:example) do
raise "this error"
end
it "is reported as failure" do
end
end
"""
When I run `rspec error_in_before_example_spec.rb`
Then the output should contain "1 example, 1 failure"
And the output should contain "this error"