File: before_and_after_hooks.feature

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (476 lines) | stat: -rw-r--r-- 12,781 bytes parent folder | download | duplicates (2)
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
466
467
468
469
470
471
472
473
474
475
476
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
  ```

  A bare `before` or `after` hook defaults to the `:example` scope.

  `before` and `after` hooks can be defined directly in the example groups they
  should run in, or in a global `RSpec.configure` block. Note that the status of
  the example does not affect the hooks.

  **WARNING:** Setting instance variables are not supported in `before(:suite)`.

  **WARNING:** Mocks are only supported in `before(:example)`.

  **WARNING:** `around` hooks will execute *before* any `before` hooks, and *after*
  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: A failure in an example does not affect hooks
    Given a file named "failure_in_example_spec.rb" with:
      """ruby
      RSpec.describe "a failing example does not affect hooks" do
        before(:context) { puts "before context runs" }
        before(:example) { puts "before example runs" }
        after(:example) { puts "after example runs" }
        after(:context) { puts "after context runs" }

        it "fails the example but runs the hooks" do
          raise "An Error"
        end
      end
      """
    When I run `rspec failure_in_example_spec.rb`
    Then it should fail with:
      """
      before context runs
      before example runs
      after example runs
      Fafter context runs
      """

  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

        before do
          puts "also before example but by default"
        end

        after(:example) do
          puts "after example"
        end

        after do
          puts "also after example but by default"
        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
      also before example but by default
      also after example but by default
      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: `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"