Skip to content

Commit ecb39f1

Browse files
author
Stanislav (Stas) Katkov
committed
cleaning up a class
1 parent 980ec9a commit ecb39f1

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

lib/rspec/rails/matchers/have_reported_error.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ def matches?(block)
6161
return attributes_match_if_specified?
6262
end
6363

64+
# @private
6465
def supports_block_expectations?
6566
true
6667
end
6768

69+
# @private
6870
def supports_value_expectations?
6971
false
7072
end
@@ -93,12 +95,8 @@ def description
9395
end
9496

9597
def failure_message
96-
if !@reports.empty? && !@attributes.empty?
97-
report_context = @reports.last.context
98-
unmatched = unmatched_attributes(report_context)
99-
unless unmatched.empty?
100-
return "Expected error attributes to match #{@attributes}, but got these mismatches: #{unmatched} and actual values are #{report_context}"
101-
end
98+
if @reports.any? && @attributes.any?
99+
return "Expected error attributes to match #{@attributes}, but actual values are #{matching_report.context}"
102100
elsif @reports.empty?
103101
return 'Expected the block to report an error, but none was reported.'
104102
elsif actual_error.nil?
@@ -136,7 +134,7 @@ def failure_message_when_negated
136134
private
137135

138136
def error_matches_expectation?
139-
return true if @expected_error.nil? && @expected_message.nil? && @reports.count.positive?
137+
return true if @expected_error.nil? && @expected_message.nil?
140138

141139
@reports.any? do |report|
142140
error_class_matches?(report.error) && error_message_matches?(report.error)
@@ -170,7 +168,7 @@ def attributes_match_if_specified?
170168
end
171169

172170
def actual_error
173-
@actual_error ||= matching_report&.error
171+
matching_report&.error
174172
end
175173

176174
def matching_report

spec/rspec/rails/matchers/have_reported_error_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ class AnotherTestError < StandardError; end
9595
expect {
9696
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "actual" })
9797
}.to have_reported_error.with_context(user_id: 456, context: "expected")
98-
}.to fail_with(/Expected error attributes to match.*user_id.*456.*context.*expected.*got these mismatches.*user_id.*456.*context.*expected.*actual values are.*user_id.*123.*context.*actual/)
98+
}.to fail_with(/Expected error attributes to match/)
9999
end
100100

101101
it "identifies partial attribute mismatches correctly" do
102102
expect {
103103
expect {
104104
Rails.error.report(StandardError.new("test"), context: { user_id: 123, status: "active", role: "admin" })
105105
}.to have_reported_error.with_context(user_id: 456, status: "active") # user_id wrong, status correct
106-
}.to fail_with(/got these mismatches:.*user_id.*456/)
106+
}.to fail_with(/Expected error attributes to match/)
107107
end
108108

109109
it "handles RSpec matcher mismatches in failure messages" do
@@ -151,7 +151,7 @@ class AnotherTestError < StandardError; end
151151
expect {
152152
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "actual" })
153153
}.to have_reported_error.with_context(user_id: 456, context: "expected")
154-
}.to fail_with(/Expected error attributes to match.*user_id.*456.*context.*expected.*got these mismatches.*user_id.*456.*context.*expected.*actual values are.*user_id.*123.*context.*actual/)
154+
}.to fail_with(/Expected error attributes to match/)
155155
end
156156

157157
it "fails when no error is reported but attributes are expected" do

0 commit comments

Comments
 (0)