@@ -23,22 +23,16 @@ def report(error, **attrs)
2323 # @api private
2424 # @see RSpec::Rails::Matchers#have_reported_error
2525 class HaveReportedError < RSpec ::Rails ::Matchers ::BaseMatcher
26- def initialize ( expected_error_class = nil , expected_message = nil )
27- # Handle backward compatibility with old API
28- if expected_error_class . is_a? ( Exception )
29- @expected_error_class = expected_error_class . class
30- @expected_message = expected_error_class . message . empty? ? nil : expected_error_class . message
31- elsif expected_error_class . is_a? ( Regexp )
26+ def initialize ( expected_error_or_message = nil , expected_message = nil )
27+ if expected_error_or_message . is_a? ( Regexp )
3228 @expected_error_class = nil
33- @expected_message = expected_error_class
34- elsif expected_error_class . is_a? ( Symbol )
35- @expected_error_symbol = expected_error_class
29+ @expected_message = expected_error_or_message
30+ elsif expected_error_or_message . is_a? ( String )
3631 @expected_error_class = nil
37- @expected_message = nil
32+ @expected_message = expected_error_or_message
3833 else
39- @expected_error_class = expected_error_class
34+ @expected_error_class = expected_error_or_message
4035 @expected_message = expected_message
41- @expected_error_symbol = nil
4236 end
4337
4438 @attributes = { }
@@ -77,22 +71,26 @@ def supports_block_expectations?
7771 end
7872
7973 def description
80- desc = "report an error"
81- if @expected_error_symbol
82- desc = "report #{ @expected_error_symbol } "
83- elsif @expected_error_class
84- desc = "report a #{ @expected_error_class } error"
85- end
86- if @expected_message
87- case @expected_message
88- when Regexp
89- desc += " with message matching #{ @expected_message } "
90- when String
91- desc += " with message '#{ @expected_message } '"
92- end
93- end
94- desc += " with #{ @attributes } " unless @attributes . empty?
95- desc
74+ base_desc = if @expected_error_class
75+ "report a #{ @expected_error_class } error"
76+ else
77+ "report an error"
78+ end
79+
80+ message_desc = if @expected_message
81+ case @expected_message
82+ when Regexp
83+ " with message matching #{ @expected_message } "
84+ when String
85+ " with message '#{ @expected_message } '"
86+ end
87+ else
88+ ""
89+ end
90+
91+ attributes_desc = @attributes . empty? ? "" : " with #{ @attributes } "
92+
93+ base_desc + message_desc + attributes_desc
9694 end
9795
9896 def failure_message
@@ -105,9 +103,7 @@ def failure_message
105103 elsif @error_subscriber . events . empty?
106104 return 'Expected the block to report an error, but none was reported.'
107105 else
108- if @expected_error_symbol
109- return "Expected error to be #{ @expected_error_symbol } , but got: #{ actual_error } "
110- elsif @expected_error_class && !actual_error . is_a? ( @expected_error_class )
106+ if @expected_error_class && !actual_error . is_a? ( @expected_error_class )
111107 return "Expected error to be an instance of #{ @expected_error_class } , but got #{ actual_error . class } with message: '#{ actual_error . message } '"
112108 elsif @expected_message
113109 case @expected_message
@@ -136,11 +132,6 @@ def error_matches_expectation?
136132 # If no events were reported, we can't match anything
137133 return false if @error_subscriber . events . empty?
138134
139- # Handle symbol matching (backward compatibility)
140- if @expected_error_symbol
141- return actual_error == @expected_error_symbol
142- end
143-
144135 # If no constraints are given, any error should match
145136 return true if @expected_error_class . nil? && @expected_message . nil?
146137
@@ -206,23 +197,25 @@ def unmatched_attributes(actual)
206197 # @example Checking for specific error class with message
207198 # expect { Rails.error.report(MyError.new("message")) }.to have_reported_error(MyError, "message")
208199 #
209- # @example Checking for specific error instance (backward compatibility )
210- # expect { Rails.error.report(MyError .new("message")) }.to have_reported_error(MyError.new(" message") )
200+ # @example Checking for error with exact message (any class )
201+ # expect { Rails.error.report(StandardError .new("exact message")) }.to have_reported_error("exact message")
211202 #
212- # @example Checking error attributes
213- # expect { Rails.error.report(StandardError.new, context: "test") }.to have_reported_error.with_context(context: " test" )
203+ # @example Checking for error with message pattern (any class)
204+ # expect { Rails.error.report(StandardError.new( "test message")) }.to have_reported_error(/ test/ )
214205 #
215- # @example Checking error message patterns
206+ # @example Checking for specific error class with message pattern
216207 # expect { Rails.error.report(StandardError.new("test message")) }.to have_reported_error(StandardError, /test/)
217- # expect { Rails.error.report(StandardError.new("test message")) }.to have_reported_error(/test/)
208+ #
209+ # @example Checking error attributes
210+ # expect { Rails.error.report(StandardError.new, context: "test") }.to have_reported_error.with_context(context: "test")
218211 #
219212 # @example Negation
220213 # expect { "safe code" }.not_to have_reported_error
221214 #
222- # @param expected_error_class [Class, Exception , Regexp, Symbol, nil] the expected error class to match , or error instance for backward compatibility
215+ # @param expected_error_or_message [Class, String , Regexp, nil] the expected error class, message string , or message pattern
223216 # @param expected_message [String, Regexp, nil] the expected error message to match
224- def have_reported_error ( expected_error_class = nil , expected_message = nil )
225- HaveReportedError . new ( expected_error_class , expected_message )
217+ def have_reported_error ( expected_error_or_message = nil , expected_message = nil )
218+ HaveReportedError . new ( expected_error_or_message , expected_message )
226219 end
227220
228221 alias_method :reports_error , :have_reported_error
0 commit comments