File: around_spec.feature

package info (click to toggle)
ruby-minitest-around 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 172 kB
  • sloc: ruby: 282; makefile: 2
file content (378 lines) | stat: -rw-r--r-- 10,609 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
Feature: `around` hooks

  `around` hooks receive the example as a block argument, extended to behave as
  a proc. This lets you define code that should be executed before and after the
  example. Of course, you can do the same thing with `before` and `after` hooks;
  and it's often cleaner to do so.

  Where `around` hooks shine is when you want to run an example within a block.
  For instance, if your database library offers a transaction method that
  receives a block, you can use an `around` to cleanly open and close the
  transaction around the example.

  **WARNING:** `around` hooks do not share state with the example the way
  `before` and `after` hooks do. This means that you cannot share instance
  variables between `around` hooks and examples.

  **WARNING:** Mock frameworks are set up and torn down within the context of
  running the example. You cannot interact with them directly in `around` hooks.

  Scenario: Use the example as a proc within the block passed to `around()`
    Given a file named "example_spec.rb" with:
      """ruby
      class Database
        def self.transaction
          puts "open transaction"
          yield
          puts "close transaction"
        end
      end

      RSpec.describe "around filter" do
        around(:example) do |example|
          Database.transaction(&example)
        end

        it "gets run in order" do
          puts "run the example"
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain:
      """
      open transaction
      run the example
      close transaction
      """

  Scenario: Invoke the example using `run()`
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "around hook" do
        around(:example) do |example|
          puts "around example before"
          example.run
          puts "around example after"
        end

        it "gets run in order" do
          puts "in the example"
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain:
      """
      around example before
      in the example
      around example after
      """

  @rspec
  Scenario: Access the example metadata
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "something" do
        around(:example) do |example|
          puts example.metadata[:foo]
          example.run
        end

        it "does something", :foo => "this should show up in the output" do
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "this should show up in the output"

  Scenario: An around hook continues to run even if the example throws an exception
    Given a file named "example_spec.rb" with:
      """ruby
        RSpec.describe "something" do
          around(:example) do |example|
            puts "around example setup"
            example.run
            puts "around example cleanup"
          end

          it "still executes the entire around hook" do
            fail "the example blows up"
          end
        end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 1 error"
    And the output should contain:
      """
      around example setup
      around example cleanup
      """

  @rspec
  Scenario: Define a global `around` hook
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.configure do |c|
        c.around(:example) do |example|
          puts "around example before"
          example.run
          puts "around example after"
        end
      end

      RSpec.describe "around filter" do
        it "gets run in order" do
          puts "in the example"
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain:
      """
      around example before
      in the example
      around example after
      """

  Scenario: Per example hooks are wrapped by the `around` hook
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "around filter" do
        around(:example) do |example|
          puts "around example before"
          example.run
          puts "around example after"
        end

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

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

        it "gets run in order" do
          puts "in the example"
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain:
      """
      around example before
      before example
      in the example
      after example
      around example after
      """

  @todo
  Scenario: Context hooks are NOT wrapped by the `around` hook
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "around filter" do
        around(:example) do |example|
          puts "around example before"
          example.run
          puts "around example after"
        end

        before(:context) do
          puts "before context"
        end

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

        it "gets run in order" do
          puts "in the example"
        end
      end
      """
    When I run `rspec --format progress example_spec.rb`
    Then the output should contain:
      """
      before context
      around example before
      in the example
      around example after
      .after context
      """

  @rspec
  Scenario: Examples run by an `around` block are run in the configured context
    Given a file named "example_spec.rb" with:
      """ruby
      module IncludedInConfigureBlock
        def included_in_configure_block; true; end
      end

      RSpec.configure do |c|
        c.include IncludedInConfigureBlock
      end

      RSpec.describe "around filter" do
        around(:example) do |example|
          example.run
        end

        it "runs the example in the correct context" do
          expect(included_in_configure_block).to be_truthy
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 0 failure"

  @rspec
  Scenario: Implicitly pending examples are detected as Not yet implemented
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "implicit pending example" do
        around(:example) do |example|
          example.run
        end

        it "should be detected as Not yet implemented"
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 0 failures, 1 pending"
    And the output should contain:
      """
      Pending: (Failures listed here are expected and do not affect your suite's status)

        1) implicit pending example should be detected as Not yet implemented
           # Not yet implemented
           # ./example_spec.rb:6
      """


  @rspec
  Scenario: Explicitly pending examples are detected as pending
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "explicit pending example" do
        around(:example) do |example|
          example.run
        end

        it "should be detected as pending" do
          pending
          fail
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 0 failures, 1 pending"
    And the output should contain:
      """
      Pending: (Failures listed here are expected and do not affect your suite's status)

        1) explicit pending example should be detected as pending
           # No reason given
      """

  Scenario: Multiple `around` hooks in the same scope
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "if there are multiple around hooks in the same scope" do
        around(:example) do |example|
          puts "first around hook before"
          example.run
          puts "first around hook after"
        end

        around(:example) do |example|
          puts "second around hook before"
          example.run
          puts "second around hook after"
        end

        it "they should all be run" do
          puts "in the example"
          expect(1).to eq(1)
        end
      end
      """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 0 failure"
    And the output should contain:
      """
      first around hook before
      second around hook before
      in the example
      second around hook after
      first around hook after
      """

  Scenario: `around` hooks in multiple scopes
    Given a file named "example_spec.rb" with:
    """ruby
    RSpec.describe "if there are around hooks in an outer scope" do
      around(:example) do |example|
        puts "first outermost around hook before"
        example.run
        puts "first outermost around hook after"
      end

      around(:example) do |example|
        puts "second outermost around hook before"
        example.run
        puts "second outermost around hook after"
      end

      describe "outer scope" do
        around(:example) do |example|
          puts "first outer around hook before"
          example.run
          puts "first outer around hook after"
        end

        around(:example) do |example|
          puts "second outer around hook before"
          example.run
          puts "second outer around hook after"
        end

        describe "inner scope" do
          around(:example) do |example|
            puts "first inner around hook before"
            example.run
            puts "first inner around hook after"
          end

          around(:example) do |example|
            puts "second inner around hook before"
            example.run
            puts "second inner around hook after"
          end

          it "they should all be run" do
            puts "in the example"
          end
        end
      end
    end
    """
    When I run `rspec example_spec.rb`
    Then the output should contain "1 example, 0 failure"
    And the output should contain:
    """
    first outermost around hook before
    second outermost around hook before
    first outer around hook before
    second outer around hook before
    first inner around hook before
    second inner around hook before
    in the example
    second inner around hook after
    first inner around hook after
    second outer around hook after
    first outer around hook after
    second outermost around hook after
    first outermost around hook after
    """