File: aggregate_failures_spec.rb

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 (163 lines) | stat: -rw-r--r-- 5,728 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
RSpec.describe "Aggregating failures" do
  shared_examples_for "failure aggregation" do |exception_attribute, example_meta|
    context "via the `aggregate_failures` method" do
      context 'when the example has an expectation failure, plus an `after` hook and an `around` hook failure' do
        it 'presents a flat list of three failures' do
          ex = nil

          RSpec.describe do
            ex = example "ex", example_meta do
              aggregate_failures { expect(1).to be_even }
            end
            after { raise "after" }
            around { |example| example.run; raise "around" }
          end.run

          expect(ex.execution_result.__send__(exception_attribute)).to have_attributes(
            :all_exceptions => [
              an_object_having_attributes(:message => /expected.*even\?/),
              an_object_having_attributes(:message => 'after'),
              an_object_having_attributes(:message => 'around')
            ]
          )
        end
      end

      context 'when the example has multiple expectation failures, plus an `after` hook and an `around` hook failure' do
        it 'nests the expectation failures so that they can be labeled with the aggregation block label' do
          ex = nil

          RSpec.describe do
            ex = example "ex", example_meta do
              aggregate_failures do
                expect(1).to be_even
                expect(2).to be_odd
              end
            end
            after { raise "after" }
            around { |example| example.run; raise "around" }
          end.run

          exception = ex.execution_result.__send__(exception_attribute)

          expect(exception).to have_attributes(
            :all_exceptions => [
              an_object_having_attributes(:class   => RSpec::Expectations::MultipleExpectationsNotMetError),
              an_object_having_attributes(:message => 'after'),
              an_object_having_attributes(:message => 'around')
            ]
          )

          expect(exception.all_exceptions.first.all_exceptions).to match [
            an_object_having_attributes(:message => /expected.*even\?/),
            an_object_having_attributes(:message => /expected.*odd\?/)
          ]
        end
      end
    end

    context "via `:aggregate_failures` metadata" do
      it 'applies `aggregate_failures` to examples or groups tagged with `:aggregate_failures`' do
        ex = nil

        RSpec.describe "Aggregate failures", :aggregate_failures do
          ex = it "has multiple failures", example_meta do
            expect(1).to be_even
            expect(2).to be_odd
          end
        end.run

        expect(ex.execution_result).not_to be_pending_fixed
        expect(ex.execution_result.status).to eq(:pending) if example_meta.key?(:pending)
        expect(ex.execution_result.__send__(exception_attribute)).to have_attributes(
          :all_exceptions => [
            an_object_having_attributes(:message => /expected.*even\?/),
            an_object_having_attributes(:message => /expected.*odd\?/)
          ]
        )
      end

      context 'when the example has an exception, plus another error' do
        it 'reports it as a multiple exception error' do
          ex = nil

          RSpec.describe "Aggregate failures", :aggregate_failures do
            ex = example "fail and raise", example_meta do
              expect(1).to be_even
              boom
            end
          end.run

          expect(ex.execution_result.__send__(exception_attribute)).to have_attributes(
            :all_exceptions => [
              an_object_having_attributes(:message => /expected.*even\?/),
              an_object_having_attributes(:class => NameError, :message => /boom/)
            ]
          )
        end
      end

      context 'when the example has multiple exceptions, plus another error' do
        it 'reports it as a flat multiple exception error' do
          ex = nil

          RSpec.describe "Aggregate failures", :aggregate_failures do
            ex = example "fail and raise", example_meta do
              expect(1).to be_even
              expect(2).to be_odd
              boom
            end
          end.run

          expect(ex.execution_result.__send__(exception_attribute)).to have_attributes(
            :all_exceptions => [
              an_object_having_attributes(:message => /expected.*even\?/),
              an_object_having_attributes(:message => /expected.*odd\?/),
              an_object_having_attributes(:class => NameError, :message => /boom/)
            ]
          )
        end
      end
    end
  end

  context "for a non-pending example" do
    include_examples "failure aggregation", :exception, {}

    it 'does not interfere with other `around` hooks' do
      events = []

      RSpec.describe "Outer" do
        around do |ex|
          events << :outer_before
          ex.run
          events << :outer_after
        end

        context "aggregating failures", :aggregate_failures do
          context "inner" do
            around do |ex|
              events << :inner_before
              ex.run
              events << :inner_after
            end

            it "has multiple failures" do
              events << :example_before
              expect(1).to be_even
              expect(2).to be_odd
              events << :example_after
            end
          end
        end
      end.run

      expect(events).to eq([:outer_before, :inner_before, :example_before,
                            :example_after, :inner_after, :outer_after])
    end
  end

  context "for a pending example" do
    include_examples "failure aggregation", :pending_exception, :pending => true
  end
end