Skip to content

Commit

Permalink
Merge pull request #1456 from nevinera/nev--give-correct-message-for-…
Browse files Browse the repository at this point in the history
…regex-not-raise-case

Give correct error message for `expect {}.not_to raise_error(/foo/)`
  • Loading branch information
pirj committed May 11, 2024
2 parents 94f00e3 + 32ea28c commit 261d7de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/matchers/built_in/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
23 changes: 19 additions & 4 deletions spec/rspec/matchers/built_in/raise_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down

0 comments on commit 261d7de

Please sign in to comment.