@@ -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
0 commit comments