Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/raise_error_spec.rb

raw symbols SCIP
1 RSpec.describe "expect { ... }.to raise_error" do
2 it_behaves_like "an RSpec block-only matcher" do
3 def valid_block
4 raise "boom"
5 end
6 def invalid_block
7 end
8 let(:matcher) { raise_error Exception }
9 end
10
11 it "passes if anything is raised" do
12 expect { raise "error" }.to raise_error "error"
13 end
14
15 it "issues a warning when used without an error class or message" do
16 expect_warning_with_call_site __FILE__, __LINE__+1, /without providing a specific error/
17 expect { raise }.to raise_error
18 end
19
20 it 'issues a warning that includes the current error when used without an error class or message' do
21 expect_warning_with_call_site __FILE__, __LINE__+1, /Actual error raised was #<StandardError: boom>/
22 expect { raise StandardError.new, 'boom' }.to raise_error
23 end
24
25 it "issues a warning when `nil` is passed for an error class" do
26 expect_warning_with_call_site __FILE__, __LINE__+1, /with a `nil`/
27 expect { raise }.to raise_error(nil)
28 end
29
30 it "issues a warning when `nil` is passed for an error class when negated" do
31 expect_warning_with_call_site __FILE__, __LINE__+1, /raise_error\(nil\)/
32 expect { '' }.not_to raise_error(nil)
33 end
34
35 it "issues a warning that does not include current error when it's not present" do
36 expect(::Kernel).to receive(:warn) do |message|
37 ex = /Actual error raised was/
38 expect(message).not_to match ex
39 end
40
41 expect {
42 expect { '' }.to(raise_error)
43 }.to fail_with("expected Exception but nothing was raised")
44 end
45
46 it "raises an exception when configured to do so" do
47 begin
48 RSpec::Expectations.configuration.on_potential_false_positives = :raise
49 expect_no_warnings
50 expect { expect { '' }.to raise_error }.to raise_error ArgumentError
51 ensure
52 RSpec::Expectations.configuration.on_potential_false_positives = :warn
53 end
54 end
55
56 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
57 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
58 expect_no_warnings
59 expect { raise }.to raise_error
60 end
61
62 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
63 RSpec::Expectations.configuration.on_potential_false_positives = :nothing
64 expect_no_warnings
65 expect { raise }.to raise_error
66 end
67
68 it 'does not issue a warning when an exception class is specified (even if it is just `Exception`)' do
69 expect_no_warnings
70 expect { raise "error" }.to raise_error Exception
71 end
72
73 it 'does not issue a warning when a message is specified' do
74 expect_no_warnings
75 expect { raise "error" }.to raise_error "error"
76 end
77
78 it 'does not issue a warning when a block is passed' do
79 expect_no_warnings
80 expect { raise "error" }.to raise_error { |_| }
81 end
82
83 it "passes if an error instance is expected" do
84 s = StandardError.new
85 expect { raise s }.to raise_error(s)
86 end
87
88 it 'passes if an error instance with a non string message is raised' do
89 special_error =
90 Class.new(StandardError) do
91 def initialize(message)
92 @message = message
93 end
94
95 def message
96 self
97 end
98
99 def to_s
100 @message
101 end
102 end
103 s = special_error.new 'Stringlike'
104 expect { raise s }.to raise_error('Stringlike')
105 end
106
107 it "fails if a different error instance is thrown from the one that is expected" do
108 s = StandardError.new("Error 1")
109 to_raise = StandardError.new("Error 2")
110 expect do
111 expect { raise to_raise }.to raise_error(s)
112 end.to fail_with(Regexp.new("expected #{s.inspect}, got #{to_raise.inspect} with backtrace"))
113 end
114
115 it "passes if an error class is expected and an instance of that class is thrown" do
116 s = StandardError.new :bees
117
118 expect { raise s }.to raise_error(StandardError)
119 end
120
121 it "fails if nothing is raised" do
122 expect {
123 expect {}.to raise_error Exception
124 }.to fail_with("expected Exception but nothing was raised")
125 end
126 end
127
128 RSpec.describe "raise_exception aliased to raise_error" do
129 it "passes if anything is raised" do
130 expect { raise "exception" }.to raise_exception "exception"
131 end
132 end
133
134 RSpec.describe "expect { ... }.to raise_error {|err| ... }" do
135 it "passes if there is an error" do
136 ran = false
137 expect { non_existent_method }.to raise_error { |_e|
138 ran = true
139 }
140 expect(ran).to be_truthy
141 end
142
143 it "passes the error to the block" do
144 error = nil
145 expect { non_existent_method }.to raise_error { |e|
146 error = e
147 }
148 expect(error).to be_kind_of(NameError)
149 end
150 end
151
152 RSpec.describe "expect { ... }.to raise_error do |err| ... end" do
153 it "passes the error to the block" do
154 error = nil
155 expect { non_existent_method }.to raise_error do |e|
156 error = e
157 end
158 expect(error).to be_kind_of(NameError)
159 end
160 end
161
162 RSpec.describe "expect { ... }.to(raise_error { |err| ... }) do |err| ... end" do
163 it "passes the error only to the block taken directly by #raise_error" do
164 error_passed_to_curly = nil
165 error_passed_to_do_end = nil
166
167 expect { non_existent_method }.to(raise_error { |e| error_passed_to_curly = e }) do |e|
168 error_passed_to_do_end = e
169 end
170
171 expect(error_passed_to_curly).to be_kind_of(NameError)
172 expect(error_passed_to_do_end).to be_nil
173 end
174 end
175
176 # rubocop:disable Style/RedundantException
177 RSpec.describe "expect { ... }.not_to raise_error" do
178
179 context "with a specific error class" do
180 it "issues a warning" do
181 expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/
182 expect { "bees" }.not_to raise_error(RuntimeError)
183 end
184
185 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
186 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
187 expect_no_warnings
188 expect { "bees" }.not_to raise_error(RuntimeError)
189 end
190 end
191
192 context "with no specific error class" do
193 it "passes if nothing is raised" do
194 expect {}.not_to raise_error
195 end
196
197 it "fails if anything is raised" do
198 expect {
199 expect { raise RuntimeError, "example message" }.not_to raise_error
200 }.to fail_with(/expected no Exception, got #<RuntimeError: example message>/)
201 end
202
203 it 'includes the backtrace of the error that was raised in the error message' do
204 expect {
205 expect { raise "boom" }.not_to raise_error
206 }.to raise_error { |e|
207 backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
208 expect(e.message).to include("with backtrace", backtrace_line)
209 }
210 end
211
212 it 'formats the backtrace using the configured backtrace formatter' do
213 allow(RSpec::Matchers.configuration.backtrace_formatter).
214 to receive(:format_backtrace).
215 and_return("formatted-backtrace")
216
217 expect {
218 expect { raise "boom" }.not_to raise_error
219 }.to raise_error { |e|
220 expect(e.message).to include("with backtrace", "formatted-backtrace")
221 }
222 end
223 end
224 end
225
226 RSpec.describe "expect { ... }.to raise_error(message)" do
227 it "passes if RuntimeError is raised with the right message" do
228 expect { raise 'blah' }.to raise_error('blah')
229 end
230
231 it "passes if RuntimeError is raised with a matching message" do
232 expect { raise 'blah' }.to raise_error(/blah/)
233 end
234
235 it "passes if any other error is raised with the right message" do
236 expect { raise NameError.new('blah') }.to raise_error('blah')
237 end
238
239 it "fails if RuntimeError error is raised with the wrong message" do
240 expect do
241 expect { raise 'blarg' }.to raise_error('blah')
242 end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
243 end
244
245 it "fails if any other error is raised with the wrong message" do
246 expect do
247 expect { raise NameError.new('blarg') }.to raise_error('blah')
248 end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg/)
249 end
250
251 it 'includes the backtrace of any other error in the failure message' do
252 expect {
253 expect { raise "boom" }.to raise_error(ArgumentError)
254 }.to raise_error { |e|
255 backtrace_line = "#{File.basename(__FILE__)}:#{__LINE__ - 2}"
256 expect(e.message).to include("with backtrace", backtrace_line)
257 }
258 end
259 end
260
261 RSpec.describe "expect { ... }.to raise_error.with_message(message)" do
262 it "raises an argument error if raise_error itself expects a message" do
263 expect {
264 expect {}.to raise_error("bees").with_message("sup")
265 }.to raise_error.with_message(/`expect \{ \}\.to raise_error\(message\)\.with_message\(message\)` is not valid/)
266 end
267
268 it "passes if RuntimeError is raised with the right message" do
269 expect { raise 'blah' }.to raise_error.with_message('blah')
270 end
271
272 it "passes if RuntimeError is raised with a matching message" do
273 expect { raise 'blah' }.to raise_error.with_message(/blah/)
274 end
275
276 it "passes if any other error is raised with the right message" do
277 expect { raise NameError.new('blah') }.to raise_error.with_message('blah')
278 end
279
280 it "fails if RuntimeError error is raised with the wrong message" do
281 expect do
282 expect { raise 'blarg' }.to raise_error.with_message('blah')
283 end.to fail_with(/expected Exception with \"blah\", got #<RuntimeError: blarg>/)
284 end
285
286 it "fails if any other error is raised with the wrong message" do
287 expect do
288 expect { raise NameError.new('blarg') }.to raise_error.with_message('blah')
289 end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg/)
290 end
291 end
292
293 RSpec.describe "expect { ... }.not_to raise_error('message')" do
294 it "issues a warning when configured to do so", :warn_about_potential_false_positives do
295 RSpec::Expectations.configuration.warn_about_potential_false_positives = true
296 expect_warning_with_call_site __FILE__, __LINE__+1, /not_to raise_error\(message\)` risks false positives/
297 expect { raise 'blarg' }.not_to raise_error('blah')
298 end
299
300 it "supresses the warning when configured to do so", :warn_about_potential_false_positives do
301 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
302 expect_no_warnings
303 expect { raise 'blarg' }.not_to raise_error('blah')
304 end
305 end
306
307 RSpec.describe "expect { ... }.not_to raise_error(/message/)" do
308 it "issues a warning when configured to do so", :warn_about_potential_false_positives do
309 RSpec::Expectations.configuration.warn_about_potential_false_positives = true
310 expect_warning_with_call_site __FILE__, __LINE__+1, /not_to raise_error\(message\)` risks false positives/
311 expect { raise 'blarg' }.not_to raise_error(/blah/)
312 end
313
314 it "supresses the warning when configured to do so", :warn_about_potential_false_positives do
315 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
316 expect_no_warnings
317 expect { raise 'blarg' }.not_to raise_error(/blah/)
318 end
319 end
320
321 RSpec.describe "expect { ... }.to raise_error(NamedError)" do
322 it "passes if named error is raised" do
323 expect { non_existent_method }.to raise_error(NameError)
324 end
325
326 it "fails if nothing is raised" do
327 expect {
328 expect {}.to raise_error(NameError)
329 }.to fail_with(/expected NameError but nothing was raised/)
330 end
331
332 it "fails if another error is raised (NameError)" do
333 expect {
334 expect { raise RuntimeError, "example message" }.to raise_error(NameError)
335 }.to fail_with(/expected NameError, got #<RuntimeError: example message>/)
336 end
337
338 it "fails if another error is raised (NameError)" do
339 expect {
340 expect { load "non/existent/file" }.to raise_error(NameError)
341 }.to fail_with(/expected NameError, got #<LoadError/)
342 end
343 end
344
345 RSpec.describe "expect { ... }.not_to raise_error(NamedError)" do
346 it "issues a warning" do
347 expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/
348 expect {}.not_to raise_error(NameError)
349 end
350
351 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
352 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
353 expect_no_warnings
354 expect {}.not_to raise_error(NameError)
355 end
356 end
357
358 RSpec.describe "expect { ... }.to raise_error(NamedError, error_message) with String" do
359 it "passes if named error is raised with same message" do
360 expect { raise "example message" }.to raise_error(RuntimeError, "example message")
361 end
362
363 it "fails if nothing is raised" do
364 expect {
365 expect {}.to raise_error(RuntimeError, "example message")
366 }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
367 end
368
369 it "fails if incorrect error is raised" do
370 expect {
371 expect { raise RuntimeError, "example message" }.to raise_error(NameError, "example message")
372 }.to fail_with(/expected NameError with \"example message\", got #<RuntimeError: example message>/)
373 end
374
375 it "fails if correct error is raised with incorrect message" do
376 expect {
377 expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, "example message")
378 }.to fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
379 end
380 end
381
382 RSpec.describe "expect { ... }.not_to raise_error(NamedError, error_message) with String" do
383 it "issues a warning" do
384 expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/
385 expect {}.not_to raise_error(RuntimeError, "example message")
386 end
387
388 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
389 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
390 expect_no_warnings
391 expect {}.not_to raise_error(RuntimeError, "example message")
392 end
393 end
394
395 RSpec.describe "expect { ... }.to raise_error(NamedError, error_message) with Regexp" do
396 it "passes if named error is raised with matching message" do
397 expect { raise "example message" }.to raise_error(RuntimeError, /ample mess/)
398 end
399
400 it "fails if nothing is raised" do
401 expect {
402 expect {}.to raise_error(RuntimeError, /ample mess/)
403 }.to fail_with(/expected RuntimeError with message matching \/ample mess\/ but nothing was raised/)
404 end
405
406 it "fails if incorrect error is raised" do
407 expect {
408 expect { raise RuntimeError, "example message" }.to raise_error(NameError, /ample mess/)
409 }.to fail_with(/expected NameError with message matching \/ample mess\/, got #<RuntimeError: example message>/)
410 end
411
412 it "fails if correct error is raised with incorrect message" do
413 expect {
414 expect { raise RuntimeError.new("not the example message") }.to raise_error(RuntimeError, /less than ample mess/)
415 }.to fail_with(/expected RuntimeError with message matching \/less than ample mess\/, got #<RuntimeError: not the example message>/)
416 end
417 end
418 # rubocop:enable Style/RedundantException
419
420 RSpec.describe "expect { ... }.not_to raise_error(NamedError, error_message) with Regexp" do
421 it "issues a warning" do
422 expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/
423 expect {}.not_to raise_error(RuntimeError, /ample mess/)
424 end
425
426 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
427 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
428 expect_no_warnings
429 expect {}.not_to raise_error(RuntimeError, /ample mess/)
430 end
431 end
432
433 RSpec.describe "expect { ... }.to raise_error(NamedError, error_message) { |err| ... }" do
434 it "yields exception if named error is raised with same message" do
435 ran = false
436
437 expect {
438 raise "example message"
439 }.to raise_error(RuntimeError, "example message") { |err|
440 ran = true
441 expect(err.class).to eq RuntimeError
442 expect(err.message).to eq "example message"
443 }
444
445 expect(ran).to be(true)
446 end
447
448 it "yielded block fails on it's own right" do
449 ran, passed = false, false
450
451 expect {
452 expect {
453 raise "example message"
454 }.to raise_error(RuntimeError, "example message") { |_err|
455 ran = true
456 expect(5).to eq 4
457 passed = true
458 }
459 }.to fail_with(/expected: 4/m)
460
461 expect(ran).to be_truthy
462 expect(passed).to be_falsey
463 end
464
465 it "does NOT yield exception if no error was thrown" do
466 ran = false
467
468 expect {
469 expect {}.to raise_error(RuntimeError, "example message") { |_err|
470 ran = true
471 }
472 }.to fail_with(/expected RuntimeError with \"example message\" but nothing was raised/)
473
474 expect(ran).to eq false
475 end
476
477 it "does not yield exception if error class is not matched" do
478 ran = false
479
480 expect {
481 expect {
482 raise "example message"
483 }.to raise_error(SyntaxError, "example message") { |_err|
484 ran = true
485 }
486 }.to fail_with(/expected SyntaxError with \"example message\", got #<RuntimeError: example message>/)
487
488 expect(ran).to eq false
489 end
490
491 it "does NOT yield exception if error message is not matched" do
492 ran = false
493
494 expect {
495 expect {
496 raise "example message"
497 }.to raise_error(RuntimeError, "different message") { |_err|
498 ran = true
499 }
500 }.to fail_with(/expected RuntimeError with \"different message\", got #<RuntimeError: example message>/)
501
502 expect(ran).to eq false
503 end
504 end
505
506 RSpec.describe "expect { ... }.not_to raise_error(NamedError, error_message) { |err| ... }" do
507 it "issues a warning" do
508 expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/
509 expect {}.not_to raise_error(RuntimeError, "example message") { |err| }
510 end
511
512 it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do
513 RSpec::Expectations.configuration.warn_about_potential_false_positives = false
514 expect_no_warnings
515 expect {}.not_to raise_error(RuntimeError, "example message") { |err| }
516 end
517 end
518
519 RSpec.describe "Composing matchers with `raise_error`" do
520 matcher :an_attribute do |attr|
521 chain :equal_to do |value|
522 @expected_value = value
523 end
524
525 match do |error|
526 return false unless error.respond_to?(attr)
527 error.__send__(attr) == @expected_value
528 end
529 end
530
531 class FooError < StandardError
532 def foo; :bar; end
533 end
534
535 describe "expect { }.to raise_error(matcher)" do
536 it 'passes when the matcher matches the raised error' do
537 expect { raise FooError }.to raise_error(an_attribute(:foo).equal_to(:bar))
538 end
539
540 it 'passes when the matcher matches the exception message' do
541 expect { raise FooError, "food" }.to raise_error(a_string_including("foo"))
542 end
543
544 it 'fails with a clear message when the matcher does not match the raised error' do
545 expect {
546 expect { raise FooError }.to raise_error(an_attribute(:foo).equal_to(3))
547 }.to fail_including("expected Exception with an attribute :foo equal to 3, got #<FooError: FooError>")
548 end
549
550 it 'fails with a clear message when the matcher does not match the exception message' do
551 expect {
552 expect { raise FooError, "food" }.to raise_error(a_string_including("bar"))
553 }.to fail_including('expected Exception with a string including "bar", got #<FooError: food')
554 end
555
556 it 'provides a description' do
557 description = raise_error(an_attribute(:foo).equal_to(3)).description
558 expect(description).to eq("raise Exception with an attribute :foo equal to 3")
559 end
560 end
561
562 describe "expect { }.to raise_error(ErrorClass, matcher)" do
563 it 'passes when the class and matcher match the raised error' do
564 expect { raise FooError, "food" }.to raise_error(FooError, a_string_including("foo"))
565 end
566
567 it 'fails with a clear message when the matcher does not match the raised error' do
568 expect {
569 expect { raise FooError, "food" }.to raise_error(FooError, a_string_including("bar"))
570 }.to fail_including('expected FooError with a string including "bar", got #<FooError: food')
571 end
572
573 it 'provides a description' do
574 description = raise_error(FooError, a_string_including("foo")).description
575 expect(description).to eq('raise FooError with a string including "foo"')
576 end
577 end
578
579 describe "expect { }.to raise_error(ErrorClass).with_message(matcher)" do
580 it 'passes when the class and matcher match the raised error' do
581 expect { raise FooError, "food" }.to raise_error(FooError).with_message(a_string_including("foo"))
582 end
583
584 it 'fails with a clear message when the matcher does not match the raised error' do
585 expect {
586 expect { raise FooError, "food" }.to raise_error(FooError).with_message(a_string_including("bar"))
587 }.to fail_including('expected FooError with a string including "bar", got #<FooError: food')
588 end
589
590 it 'provides a description' do
591 description = raise_error(FooError).with_message(a_string_including("foo")).description
592 expect(description).to eq('raise FooError with a string including "foo"')
593 end
594 end
595 end