Skip to content

Commit c1816e2

Browse files
committed
warn about nil being passed
1 parent 2488a71 commit c1816e2

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/rspec/rails/matchers/have_reported_error.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class HaveReportedError < RSpec::Rails::Matchers::BaseMatcher
3737
# Expected message when first param is a class
3838
def initialize(expected_error_or_message = UndefinedValue, expected_message = nil)
3939
@attributes = {}
40+
@warn_about_nil_error = expected_error_or_message.nil?
4041

4142
case expected_error_or_message
42-
when nil, UndefinedValue
43+
when UndefinedValue
4344
@expected_error = nil
4445
@expected_message = expected_message
4546
when String, Regexp
@@ -65,6 +66,8 @@ def matches?(block)
6566
raise ArgumentError, "this matcher doesn't work with value expectations"
6667
end
6768

69+
warn_about_nil_error! if @warn_about_nil_error
70+
6871
@error_subscriber = ErrorSubscriber.new
6972
::Rails.error.subscribe(@error_subscriber)
7073

@@ -213,6 +216,18 @@ def unmatched_attributes(actual)
213216
end
214217
end
215218
end
219+
220+
def warn_about_nil_error!
221+
RSpec.warn_with("Using the `have_reported_error` matcher with a `nil` error is probably " \
222+
"unintentional, it risks false positives, since `have_reported_error` " \
223+
"will match when any error is reported to Rails.error, potentially " \
224+
"allowing the expectation to pass without the specific error you are " \
225+
"intending to test for being reported. " \
226+
"Instead consider providing a specific error class or message. " \
227+
"This message can be suppressed by setting: " \
228+
"`RSpec::Expectations.configuration.on_potential_false" \
229+
"_positives = :nothing`")
230+
end
216231
end
217232

218233
# @api public

spec/rspec/rails/matchers/have_reported_error_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ class AnotherTestError < StandardError; end
1717
expect {Rails.error.report(StandardError.new("test error"))}.to have_reported_error
1818
end
1919

20-
it "passes when an error is reported with explicit nil argument" do
21-
expect {Rails.error.report(StandardError.new("test error"))}.to have_reported_error(nil)
20+
it "fails when an error is reported with explicit nil argument" do
21+
expect {
22+
expect {Rails.error.report(StandardError.new("test error"))}.to have_reported_error(nil)
23+
}.to raise_error(RuntimeError, /Using the `have_reported_error` matcher with a `nil` error is probably unintentional/)
2224
end
2325

2426
it "fails when no errors are reported" do
@@ -29,7 +31,7 @@ class AnotherTestError < StandardError; end
2931

3032
it "fails when no errors are reported with explicit nil argument" do
3133
expect {
32-
expect { "no error" }.to have_reported_error(nil)
34+
expect { "no error" }.to have_reported_error
3335
}.to fail_with(/Expected the block to report an error, but none was reported./)
3436
end
3537

0 commit comments

Comments
 (0)