File: pending_example_spec.rb

package info (click to toggle)
ruby-rspec 3.8.0c0e1m0s0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,640 kB
  • sloc: ruby: 65,844; sh: 807; makefile: 99
file content (221 lines) | stat: -rw-r--r-- 6,285 bytes parent folder | download | duplicates (3)
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
RSpec.describe "an example" do
  context "declared pending with metadata" do
    it "uses the value assigned to :pending as the message" do
      group = RSpec.describe('group') do
        example "example", :pending => 'just because' do
          fail
        end
      end
      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_pending_with('just because')
    end

    it "sets the message to 'No reason given' if :pending => true" do
      group = RSpec.describe('group') do
        example "example", :pending => true do
          fail
        end
      end
      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_pending_with('No reason given')
    end

    it "passes if a mock expectation is not satisifed" do
      group = RSpec.describe('group') do
        example "example", :pending => "because" do
          expect(RSpec).to receive(:a_message_in_a_bottle)
        end
      end

      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_pending_with('because')
      expect(example.execution_result.status).to eq(:pending)
    end

    it "does not mutate the :pending attribute of the user metadata when handling mock expectation errors" do
      group = RSpec.describe('group') do
        example "example", :pending => "because" do
          expect(RSpec).to receive(:a_message_in_a_bottle)
        end
      end

      group.run
      example = group.examples.first
      expect(example.metadata[:pending]).to be_truthy
    end
  end

  context "made pending with `define_derived_metadata`" do
    before do
      RSpec.configure do |config|
        config.define_derived_metadata(:not_ready) do |meta|
          meta[:pending] ||= "Not ready"
        end
      end
    end

    it 'has a pending result if there is an error' do
      group = RSpec.describe "group" do
        example "something", :not_ready do
          boom
        end
      end

      group.run
      example = group.examples.first
      expect(example).to be_pending_with("Not ready")
    end

    it 'fails if there is no error' do
      group = RSpec.describe "group" do
        example "something", :not_ready do
        end
      end

      group.run
      example = group.examples.first
      expect(example.execution_result.status).to be(:failed)
      expect(example.execution_result.exception.message).to include("Expected example to fail")
    end
  end

  context "with no block" do
    it "is listed as pending with 'Not yet implemented'" do
      group = RSpec.describe('group') do
        it "has no block"
      end
      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_skipped_with('Not yet implemented')
    end
  end

  context "with no args" do
    it "is listed as pending with the default message" do
      group = RSpec.describe('group') do
        it "does something" do
          pending
          fail
        end
      end
      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_pending_with(RSpec::Core::Pending::NO_REASON_GIVEN)
    end

    it "fails when the rest of the example passes" do
      called = false
      group = RSpec.describe('group') do
        it "does something" do
          pending
          called = true
        end
      end

      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(called).to eq(true)
      result = example.execution_result
      expect(result.pending_fixed).to eq(true)
      expect(result.status).to eq(:failed)
    end

    it "does not mutate the :pending attribute of the user metadata when the rest of the example passes" do
      group = RSpec.describe('group') do
        it "does something" do
          pending
        end
      end

      group.run
      example = group.examples.first
      expect(example.metadata).to include(:pending => true)
    end
  end

  context "with no docstring" do
    context "declared with the pending method" do
      it "has an auto-generated description if it has an expectation" do
        ex = nil

        RSpec.describe('group') do
          it "checks something" do
            expect((3+4)).to eq(7)
          end
          ex = pending do
            expect("string".reverse).to eq("gnirts")
          end
        end.run

        expect(ex.description).to eq('should eq "gnirts"')
      end
    end

    context "after another example with some assertion" do
      it "does not show any message" do
        ex = nil

        RSpec.describe('group') do
          it "checks something" do
            expect((3+4)).to eq(7)
          end
          ex = specify do
            pending
          end
        end.run

        expect(ex.description).to match(/example at/)
      end
    end
  end

  context "with a message" do
    it "is listed as pending with the supplied message" do
      group = RSpec.describe('group') do
        it "does something" do
          pending("just because")
          fail
        end
      end
      example = group.examples.first
      example.run(group.new, double.as_null_object)
      expect(example).to be_pending_with('just because')
    end
  end

  context "with a block" do
    it "fails with an ArgumentError stating the syntax is deprecated" do
      group = RSpec.describe('group') do
        it "calls pending with a block" do
          pending("with invalid syntax") do
            :no_op
          end
          fail
        end
      end
      example = group.examples.first
      group.run
      expect(example).to fail_with ArgumentError
      expect(example.exception.message).to match(
        /Passing a block within an example is now deprecated./
      )
    end

    it "does not yield to the block" do
      example_to_have_yielded = :did_not_yield
      group = RSpec.describe('group') do
        it "calls pending with a block" do
          pending("just because") do
            example_to_have_yielded = :pending_block
          end
          fail
        end
      end
      group.run
      expect(example_to_have_yielded).to eq :did_not_yield
    end
  end
end