Skip to content

Commit 7259a25

Browse files
committed
rename .with to .with_context
1 parent af58d83 commit 7259a25

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/rspec/rails/matchers/have_reported_error.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def initialize(expected_error = nil)
2929
@error_subscriber = nil
3030
end
3131

32-
def with(expected_attributes)
32+
def with_context(expected_attributes)
3333
@attributes.merge!(expected_attributes)
3434
self
3535
end
@@ -46,7 +46,7 @@ def matches?(block)
4646
@error_subscriber = ErrorSubscriber.new
4747
::Rails.error.subscribe(@error_subscriber)
4848

49-
block&.call
49+
block.call
5050

5151
return false if @error_subscriber.events.empty? && !@expected_error.nil?
5252
return false unless error_matches_expectation?
@@ -176,7 +176,7 @@ def unmatched_attributes(actual)
176176
# expect { Rails.error.report(MyError.new("message")) }.to have_reported_error(MyError.new("message"))
177177
#
178178
# @example Checking error attributes
179-
# expect { Rails.error.report(StandardError.new, context: "test") }.to have_reported_error.with(context: "test")
179+
# expect { Rails.error.report(StandardError.new, context: "test") }.to have_reported_error.with_context(context: "test")
180180
#
181181
# @example Checking error message patterns
182182
# expect { Rails.error.report(StandardError.new("test message")) }.to have_reported_error(/test/)

spec/rspec/rails/matchers/have_reported_error_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,69 +85,69 @@ class AnotherTestError < StandardError; end
8585
expect {
8686
expect {
8787
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "actual" })
88-
}.to have_reported_error.with(user_id: 456, context: "expected")
88+
}.to have_reported_error.with_context(user_id: 456, context: "expected")
8989
}.to fail_with(/Expected error attributes to match {user_id: 456, context: "expected"}, but got these mismatches: {user_id: 456, context: "expected"} and actual values are {"user_id" => 123, "context" => "actual"}/)
9090
end
9191

9292
it "identifies partial attribute mismatches correctly" do
9393
expect {
9494
expect {
9595
Rails.error.report(StandardError.new("test"), context: { user_id: 123, status: "active", role: "admin" })
96-
}.to have_reported_error.with(user_id: 456, status: "active") # user_id wrong, status correct
96+
}.to have_reported_error.with_context(user_id: 456, status: "active") # user_id wrong, status correct
9797
}.to fail_with(/got these mismatches: {user_id: 456}/)
9898
end
9999

100100
it "handles RSpec matcher mismatches in failure messages" do
101101
expect {
102102
expect {
103103
Rails.error.report(StandardError.new("test"), context: { params: { foo: "different" } })
104-
}.to have_reported_error.with(params: a_hash_including(foo: "bar"))
104+
}.to have_reported_error.with_context(params: a_hash_including(foo: "bar"))
105105
}.to fail_with(/Expected error attributes to match/)
106106
end
107107

108108
it "shows actual context values when attributes don't match" do
109109
expect {
110110
expect {
111111
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "actual" })
112-
}.to have_reported_error.with(user_id: 456)
112+
}.to have_reported_error.with_context(user_id: 456)
113113
}.to fail_with(/actual values are {"user_id" => 123, "context" => "actual"}/)
114114
end
115115
end
116116

117-
describe "#with" do
117+
describe "#with_context" do
118118
it "passes when attributes match exactly" do
119119
expect {
120120
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "test" })
121-
}.to have_reported_error.with(user_id: 123, context: "test")
121+
}.to have_reported_error.with_context(user_id: 123, context: "test")
122122
end
123123

124124
it "passes with partial attribute matching" do
125125
expect {
126126
Rails.error.report(
127127
StandardError.new("test"), context: { user_id: 123, context: "test", extra: "data" }
128128
)
129-
}.to have_reported_error.with(user_id: 123)
129+
}.to have_reported_error.with_context(user_id: 123)
130130
end
131131

132132
it "passes with hash matching using RSpec matchers" do
133133
expect {
134134
Rails.error.report(
135135
StandardError.new("test"), context: { params: { foo: "bar", baz: "qux" } }
136136
)
137-
}.to have_reported_error.with(params: a_hash_including(foo: "bar"))
137+
}.to have_reported_error.with_context(params: a_hash_including(foo: "bar"))
138138
end
139139

140140
it "fails when attributes do not match" do
141141
expect {
142142
expect {
143143
Rails.error.report(StandardError.new("test"), context: { user_id: 123, context: "actual" })
144-
}.to have_reported_error.with(user_id: 456, context: "expected")
144+
}.to have_reported_error.with_context(user_id: 456, context: "expected")
145145
}.to fail_with(/Expected error attributes to match {user_id: 456, context: "expected"}, but got these mismatches: {user_id: 456, context: "expected"} and actual values are {"user_id" => 123, "context" => "actual"}/)
146146
end
147147

148148
it "fails when no error is reported but attributes are expected" do
149149
expect {
150-
expect { "no error" }.to have_reported_error.with(user_id: 123)
150+
expect { "no error" }.to have_reported_error.with_context(user_id: 123)
151151
}.to fail_with(/Expected the block to report an error, but none was reported./)
152152
end
153153
end

0 commit comments

Comments
 (0)