File: aggregating_failures.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 (602 lines) | stat: -rw-r--r-- 22,546 bytes parent folder | download
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
Feature: Aggregating Failures

  RSpec::Expectations provides [`aggregate_failures`](../../rspec-expectations/aggregating-failures), an API that allows you to group a set of expectations and see all the failures at once, rather than it aborting on the first failure. RSpec::Core improves on this feature in a couple of ways:

    * RSpec::Core provides much better failure output, adding code snippets and backtraces to the sub-failures, just like it does for any normal failure.
    * RSpec::Core provides [metadata](../metadata/user-defined-metadata) integration for this feature. Each example that is tagged with `:aggregate_failures` will be wrapped in an `aggregate_failures` block. You can also use `config.define_derived_metadata` to apply this to every example automatically.

  The metadata form is quite convenient, but may not work well for end-to-end tests that have multiple distinct steps. For example, consider a spec for an HTTP client workflow that (1) makes a request, (2) expects a redirect, (3) follows the redirect, and (4) expects a particular response.  You probably want the `expect(response.status).to be_between(300, 399)` expectation to immediately abort if it fails, because you can't perform the next step (following the redirect) if that is not satisfied. For these situations, we encourage you to use the `aggregate_failures` block form to wrap each set of expectations that represents a distinct step in the test workflow.

  Background:
    Given a file named "lib/client.rb" with:
      """ruby
      Response = Struct.new(:status, :headers, :body)

      class Client
        def self.make_request(url='/')
          Response.new(404, { "Content-Type" => "text/plain" }, "Not Found")
        end
      end
      """

  @skip-when-diff-lcs-1.3
  Scenario: Use `aggregate_failures` block form
    Given a file named "spec/use_block_form_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        after do
          # this should be appended to failure list
          expect(false).to be(true), "after hook failure"
        end

        around do |ex|
          ex.run
          # this should also be appended to failure list
          expect(false).to be(true), "around hook failure"
        end

        it "returns a successful response" do
          response = Client.make_request

          aggregate_failures "testing response" do
            expect(response.status).to eq(200)
            expect(response.headers).to include("Content-Type" => "application/json")
            expect(response.body).to eq('{"message":"Success"}')
          end
        end
      end
      """
    When I run `rspec spec/use_block_form_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Got 3 failures from failure aggregation block "testing response".
                # ./spec/use_block_form_spec.rb:18
                # ./spec/use_block_form_spec.rb:10

                1.1.1) Failure/Error: expect(response.status).to eq(200)

                         expected: 200
                              got: 404

                         (compared using ==)
                       # ./spec/use_block_form_spec.rb:19

                1.1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                         Diff:
                         @@ -1 +1 @@
                         -"Content-Type" => "application/json",
                         +"Content-Type" => "text/plain",
                       # ./spec/use_block_form_spec.rb:20

                1.1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                         expected: "{\"message\":\"Success\"}"
                              got: "Not Found"

                         (compared using ==)
                       # ./spec/use_block_form_spec.rb:21

           1.2) Failure/Error: expect(false).to be(true), "after hook failure"
                  after hook failure
                # ./spec/use_block_form_spec.rb:6
                # ./spec/use_block_form_spec.rb:10

           1.3) Failure/Error: expect(false).to be(true), "around hook failure"
                  around hook failure
                # ./spec/use_block_form_spec.rb:12
      """

  @skip-when-diff-lcs-1.4
  Scenario: Use `aggregate_failures` block form
    Given a file named "spec/use_block_form_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        after do
          # this should be appended to failure list
          expect(false).to be(true), "after hook failure"
        end

        around do |ex|
          ex.run
          # this should also be appended to failure list
          expect(false).to be(true), "around hook failure"
        end

        it "returns a successful response" do
          response = Client.make_request

          aggregate_failures "testing response" do
            expect(response.status).to eq(200)
            expect(response.headers).to include("Content-Type" => "application/json")
            expect(response.body).to eq('{"message":"Success"}')
          end
        end
      end
      """
    When I run `rspec spec/use_block_form_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Got 3 failures from failure aggregation block "testing response".
                # ./spec/use_block_form_spec.rb:18
                # ./spec/use_block_form_spec.rb:10

                1.1.1) Failure/Error: expect(response.status).to eq(200)

                         expected: 200
                              got: 404

                         (compared using ==)
                       # ./spec/use_block_form_spec.rb:19

                1.1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                         Diff:
                         @@ -1,2 +1,2 @@
                         -"Content-Type" => "application/json",
                         +"Content-Type" => "text/plain",
                       # ./spec/use_block_form_spec.rb:20

                1.1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                         expected: "{\"message\":\"Success\"}"
                              got: "Not Found"

                         (compared using ==)
                       # ./spec/use_block_form_spec.rb:21

           1.2) Failure/Error: expect(false).to be(true), "after hook failure"
                  after hook failure
                # ./spec/use_block_form_spec.rb:6
                # ./spec/use_block_form_spec.rb:10

           1.3) Failure/Error: expect(false).to be(true), "around hook failure"
                  around hook failure
                # ./spec/use_block_form_spec.rb:12
      """

  Scenario: Use `:aggregate_failures` metadata
    Given a file named "spec/use_metadata_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        it "follows a redirect", :aggregate_failures do
          response = Client.make_request

          expect(response.status).to eq(302)
          expect(response.body).to eq('{"message":"Redirect"}')

          redirect_response = Client.make_request(response.headers.fetch('Location'))

          expect(redirect_response.status).to eq(200)
          expect(redirect_response.body).to eq('{"message":"OK"}')
        end
      end
      """
    When I run `rspec spec/use_metadata_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client follows a redirect
           Got 2 failures and 1 other error:

           1.1) Failure/Error: expect(response.status).to eq(302)

                  expected: 302
                       got: 404

                  (compared using ==)
                # ./spec/use_metadata_spec.rb:7

           1.2) Failure/Error: expect(response.body).to eq('{"message":"Redirect"}')

                  expected: "{\"message\":\"Redirect\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/use_metadata_spec.rb:8

           1.3) Failure/Error: redirect_response = Client.make_request(response.headers.fetch('Location'))
                KeyError:
                  key not found: "Location"
                # ./spec/use_metadata_spec.rb:10
                # ./spec/use_metadata_spec.rb:10
      """

  @skip-when-diff-lcs-1.3
  Scenario: Enable failure aggregation globally using `define_derived_metadata`
    Given a file named "spec/enable_globally_spec.rb" with:
      """ruby
      require 'client'

      RSpec.configure do |c|
        c.define_derived_metadata do |meta|
          meta[:aggregate_failures] = true
        end
      end

      RSpec.describe Client do
        it "returns a successful response" do
          response = Client.make_request

          expect(response.status).to eq(200)
          expect(response.headers).to include("Content-Type" => "application/json")
          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/enable_globally_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/enable_globally_spec.rb:13

           1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                  expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                  Diff:
                  @@ -1 +1 @@
                  -"Content-Type" => "application/json",
                  +"Content-Type" => "text/plain",
                # ./spec/enable_globally_spec.rb:14

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/enable_globally_spec.rb:15
      """

  @skip-when-diff-lcs-1.4
  Scenario: Enable failure aggregation globally using `define_derived_metadata`
    Given a file named "spec/enable_globally_spec.rb" with:
      """ruby
      require 'client'

      RSpec.configure do |c|
        c.define_derived_metadata do |meta|
          meta[:aggregate_failures] = true
        end
      end

      RSpec.describe Client do
        it "returns a successful response" do
          response = Client.make_request

          expect(response.status).to eq(200)
          expect(response.headers).to include("Content-Type" => "application/json")
          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/enable_globally_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/enable_globally_spec.rb:13

           1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                  expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                  Diff:
                  @@ -1,2 +1,2 @@
                  -"Content-Type" => "application/json",
                  +"Content-Type" => "text/plain",
                # ./spec/enable_globally_spec.rb:14

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/enable_globally_spec.rb:15
      """

  @skip-when-diff-lcs-1.3
  Scenario: Nested failure aggregation works
    Given a file named "spec/nested_failure_aggregation_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        it "returns a successful response", :aggregate_failures do
          response = Client.make_request

          expect(response.status).to eq(200)

          aggregate_failures "testing headers" do
            expect(response.headers).to include("Content-Type" => "application/json")
            expect(response.headers).to include("Content-Length" => "21")
          end

          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/nested_failure_aggregation_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/nested_failure_aggregation_spec.rb:7

           1.2) Got 2 failures from failure aggregation block "testing headers".
                # ./spec/nested_failure_aggregation_spec.rb:9

                1.2.1) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                         Diff:
                         @@ -1 +1 @@
                         -"Content-Type" => "application/json",
                         +"Content-Type" => "text/plain",
                       # ./spec/nested_failure_aggregation_spec.rb:10

                1.2.2) Failure/Error: expect(response.headers).to include("Content-Length" => "21")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Length" => "21"}
                         Diff:
                         @@ -1 +1 @@
                         -"Content-Length" => "21",
                         +"Content-Type" => "text/plain",
                       # ./spec/nested_failure_aggregation_spec.rb:11

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/nested_failure_aggregation_spec.rb:14
      """

  @skip-when-diff-lcs-1.4
  Scenario: Nested failure aggregation works
    Given a file named "spec/nested_failure_aggregation_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        it "returns a successful response", :aggregate_failures do
          response = Client.make_request

          expect(response.status).to eq(200)

          aggregate_failures "testing headers" do
            expect(response.headers).to include("Content-Type" => "application/json")
            expect(response.headers).to include("Content-Length" => "21")
          end

          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/nested_failure_aggregation_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Client returns a successful response
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/nested_failure_aggregation_spec.rb:7

           1.2) Got 2 failures from failure aggregation block "testing headers".
                # ./spec/nested_failure_aggregation_spec.rb:9

                1.2.1) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                         Diff:
                         @@ -1,2 +1,2 @@
                         -"Content-Type" => "application/json",
                         +"Content-Type" => "text/plain",
                       # ./spec/nested_failure_aggregation_spec.rb:10

                1.2.2) Failure/Error: expect(response.headers).to include("Content-Length" => "21")
                         expected {"Content-Type" => "text/plain"} to include {"Content-Length" => "21"}
                         Diff:
                         @@ -1,2 +1,2 @@
                         -"Content-Length" => "21",
                         +"Content-Type" => "text/plain",
                       # ./spec/nested_failure_aggregation_spec.rb:11

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/nested_failure_aggregation_spec.rb:14
      """

  Scenario: Mock expectation failures are aggregated as well
    Given a file named "spec/mock_expectation_failure_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe "Aggregating Failures", :aggregate_failures do
        it "has a normal expectation failure and a message expectation failure" do
          client = double("Client")
          expect(client).to receive(:put).with("updated data")
          allow(client).to receive(:get).and_return(Response.new(404, {}, "Not Found"))

          response = client.get
          expect(response.status).to eq(200)
        end
      end
      """
    When I run `rspec spec/mock_expectation_failure_spec.rb`
    Then it should fail and list all the failures:
      """
      Failures:

        1) Aggregating Failures has a normal expectation failure and a message expectation failure
           Got 2 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/mock_expectation_failure_spec.rb:10

           1.2) Failure/Error: expect(client).to receive(:put).with("updated data")
                  (Double "Client").put("updated data")
                      expected: 1 time with arguments: ("updated data")
                      received: 0 times
                # ./spec/mock_expectation_failure_spec.rb:6

      """

  @skip-when-diff-lcs-1.3
  Scenario: Pending integrates properly with aggregated failures
    Given a file named "spec/pending_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        it "returns a successful response", :aggregate_failures do
          pending "Not yet ready"
          response = Client.make_request

          expect(response.status).to eq(200)
          expect(response.headers).to include("Content-Type" => "application/json")
          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/pending_spec.rb`
    Then it should pass and list all the pending examples:
      """
      Pending: (Failures listed here are expected and do not affect your suite's status)

        1) Client returns a successful response
           # Not yet ready
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/pending_spec.rb:8

           1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                  expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                  Diff:
                  @@ -1 +1 @@
                  -"Content-Type" => "application/json",
                  +"Content-Type" => "text/plain",
                # ./spec/pending_spec.rb:9

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/pending_spec.rb:10
      """

  @skip-when-diff-lcs-1.4
  Scenario: Pending integrates properly with aggregated failures
    Given a file named "spec/pending_spec.rb" with:
      """ruby
      require 'client'

      RSpec.describe Client do
        it "returns a successful response", :aggregate_failures do
          pending "Not yet ready"
          response = Client.make_request

          expect(response.status).to eq(200)
          expect(response.headers).to include("Content-Type" => "application/json")
          expect(response.body).to eq('{"message":"Success"}')
        end
      end
      """
    When I run `rspec spec/pending_spec.rb`
    Then it should pass and list all the pending examples:
      """
      Pending: (Failures listed here are expected and do not affect your suite's status)

        1) Client returns a successful response
           # Not yet ready
           Got 3 failures:

           1.1) Failure/Error: expect(response.status).to eq(200)

                  expected: 200
                       got: 404

                  (compared using ==)
                # ./spec/pending_spec.rb:8

           1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                  expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                  Diff:
                  @@ -1,2 +1,2 @@
                  -"Content-Type" => "application/json",
                  +"Content-Type" => "text/plain",
                # ./spec/pending_spec.rb:9

           1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                  expected: "{\"message\":\"Success\"}"
                       got: "Not Found"

                  (compared using ==)
                # ./spec/pending_spec.rb:10
      """