@@ -37,9 +37,10 @@ class HaveReportedError < RSpec::Rails::Matchers::BaseMatcher
37
37
# Expected message when first param is a class
38
38
def initialize ( expected_error_or_message = UndefinedValue , expected_message = nil )
39
39
@attributes = { }
40
+ @warn_about_nil_error = expected_error_or_message . nil?
40
41
41
42
case expected_error_or_message
42
- when nil , UndefinedValue
43
+ when UndefinedValue
43
44
@expected_error = nil
44
45
@expected_message = expected_message
45
46
when String , Regexp
@@ -65,6 +66,8 @@ def matches?(block)
65
66
raise ArgumentError , "this matcher doesn't work with value expectations"
66
67
end
67
68
69
+ warn_about_nil_error! if @warn_about_nil_error
70
+
68
71
@error_subscriber = ErrorSubscriber . new
69
72
::Rails . error . subscribe ( @error_subscriber )
70
73
@@ -213,6 +216,18 @@ def unmatched_attributes(actual)
213
216
end
214
217
end
215
218
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
216
231
end
217
232
218
233
# @api public
0 commit comments