Skip to content

Commit 7b3abc5

Browse files
committed
Using UndefinedValue as a default attribute
1 parent f5e427a commit 7b3abc5

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/rspec/rails/matchers/have_reported_error.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
module RSpec
44
module Rails
55
module Matchers
6+
# @api private
7+
# Sentinel value to distinguish between no argument passed vs explicitly passed nil.
8+
# This follows the same pattern as RSpec's raise_error matcher.
9+
UndefinedValue = Object.new.freeze
10+
611
# @api private
712
class ErrorSubscriber
813
attr_reader :events
@@ -28,17 +33,20 @@ def report(error, **attrs)
2833
class HaveReportedError < RSpec::Rails::Matchers::BaseMatcher
2934
# Initialize the matcher following raise_error patterns
3035
#
36+
# Uses UndefinedValue as default to distinguish between no argument
37+
# passed vs explicitly passed nil (same as raise_error matcher).
38+
#
3139
# @param expected_error_or_message [Class, String, Regexp, nil]
3240
# Error class, message string, or message pattern
3341
# @param expected_message [String, Regexp, nil]
3442
# Expected message when first param is a class
35-
def initialize(expected_error_or_message = nil, expected_message = nil)
43+
def initialize(expected_error_or_message = UndefinedValue, expected_message = nil)
3644
@actual_error = nil
3745
@attributes = {}
3846
@error_subscriber = nil
3947

4048
case expected_error_or_message
41-
when nil
49+
when nil, UndefinedValue
4250
@expected_error = nil
4351
@expected_message = expected_message
4452
when String, Regexp
@@ -232,7 +240,7 @@ def unmatched_attributes(actual)
232240
#
233241
# @param expected_error_or_message [Class, String, Regexp, nil] the expected error class, message string, or message pattern
234242
# @param expected_message [String, Regexp, nil] the expected error message to match
235-
def have_reported_error(expected_error_or_message = nil, expected_message = nil)
243+
def have_reported_error(expected_error_or_message = UndefinedValue, expected_message = nil)
236244
HaveReportedError.new(expected_error_or_message, expected_message)
237245
end
238246

spec/rspec/rails/matchers/have_reported_error_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ class AnotherTestError < StandardError; end
1717
expect {Rails.error.report(StandardError.new("test error"))}.to have_reported_error
1818
end
1919

20+
it "passes when an error is reported with explicit nil argument" do
21+
expect {Rails.error.report(StandardError.new("test error"))}.to have_reported_error(nil)
22+
end
23+
2024
it "fails when no errors are reported" do
2125
expect {
2226
expect { "no error" }.to have_reported_error
2327
}.to fail_with(/Expected the block to report an error, but none was reported./)
2428
end
2529

30+
it "fails when no errors are reported with explicit nil argument" do
31+
expect {
32+
expect { "no error" }.to have_reported_error(nil)
33+
}.to fail_with(/Expected the block to report an error, but none was reported./)
34+
end
35+
2636
it "passes when negated and no errors are reported" do
2737
expect { "no error" }.not_to have_reported_error
2838
end

0 commit comments

Comments
 (0)