diff --git a/Changelog.md b/Changelog.md index 230eafa46..b4e7e6b3a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ ### Development -[Full Changelog](http://github.com/rspec/rspec-expectations/compare/v3.12.3...main) +[Full Changelog](http://github.com/rspec/rspec-expectations/compare/v3.13.0...main) + +Bug Fixes: + +* Fix the false positive warning message for negated raise error with a regexp argument. + (Eric Mueller, #1456) ### 3.13.0 / 2024-02-04 [Full Changelog](http://github.com/rspec/rspec-expectations/compare/v3.12.4...v3.13.0) diff --git a/lib/rspec/matchers/built_in/raise_error.rb b/lib/rspec/matchers/built_in/raise_error.rb index 162fe056d..bbaaf623f 100644 --- a/lib/rspec/matchers/built_in/raise_error.rb +++ b/lib/rspec/matchers/built_in/raise_error.rb @@ -13,6 +13,10 @@ class RaiseError # argument. We can't use `nil` for that because we need to warn when `nil` is # passed in a different way. It's an Object, not a Module, since Module's `===` # does not evaluate to true when compared to itself. + # + # Note; this _is_ the default value supplied for expected_error_or_message, but + # because there are two method-calls involved, that default is actually supplied + # in the definition of the _matcher_ method, `RSpec::Matchers#raise_error` UndefinedValue = Object.new.freeze def initialize(expected_error_or_message, expected_message, &block) @@ -25,7 +29,7 @@ def initialize(expected_error_or_message, expected_message, &block) when nil, UndefinedValue @expected_error = Exception @expected_message = expected_message - when String + when String, Regexp @expected_error = Exception @expected_message = expected_error_or_message else diff --git a/spec/rspec/matchers/built_in/raise_error_spec.rb b/spec/rspec/matchers/built_in/raise_error_spec.rb index 5fa74ffd4..f2a388fd6 100644 --- a/spec/rspec/matchers/built_in/raise_error_spec.rb +++ b/spec/rspec/matchers/built_in/raise_error_spec.rb @@ -290,13 +290,28 @@ def to_s end end -RSpec.describe "expect { ... }.not_to raise_error(message)" do - it "issues a warning" do - expect_warning_with_call_site __FILE__, __LINE__+1, /risks false positives/ +RSpec.describe "expect { ... }.not_to raise_error('message')" do + it "issues a warning when configured to do so", :warn_about_potential_false_positives do + RSpec::Expectations.configuration.warn_about_potential_false_positives = true + expect_warning_with_call_site __FILE__, __LINE__+1, /not_to raise_error\(message\)` risks false positives/ + expect { raise 'blarg' }.not_to raise_error('blah') + end + + it "supresses the warning when configured to do so", :warn_about_potential_false_positives do + RSpec::Expectations.configuration.warn_about_potential_false_positives = false + expect_no_warnings + expect { raise 'blarg' }.not_to raise_error('blah') + end +end + +RSpec.describe "expect { ... }.not_to raise_error(/message/)" do + it "issues a warning when configured to do so", :warn_about_potential_false_positives do + RSpec::Expectations.configuration.warn_about_potential_false_positives = true + expect_warning_with_call_site __FILE__, __LINE__+1, /not_to raise_error\(message\)` risks false positives/ expect { raise 'blarg' }.not_to raise_error(/blah/) end - it "can supresses the warning when configured to do so", :warn_about_potential_false_positives do + it "supresses the warning when configured to do so", :warn_about_potential_false_positives do RSpec::Expectations.configuration.warn_about_potential_false_positives = false expect_no_warnings expect { raise 'blarg' }.not_to raise_error(/blah/)