- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
Open
Description
Feature Request
I propose to add a new spec matcher (in addition to existing #expect_raises) that would be more flexible and closer to the RSpec's #raise_error.
Examples:
# RSpec
expect { }.to raise_error
expect { }.to raise_error(ArgumentError)
expect { }.to raise_error("n must be Integer")
expect { }.to raise_error(ArgumentError, "n must be Integer")
expect { }.to raise_error(ArgumentError, /negative/)The main functionality missing in #expect_raises() is supporting arbitrary checks on a raised exception. With #raise_error you can do so with a block argument:
# RSpec
expect { Object.new.foo }.to raise_error(ResponseError) { |error|
  expect(error.code).to eq(401)
}There might be benefits in similarity with the RSpec's #raise_error.  The #expect_raises has the following semantic differences (that might confuse a one that is familiar with RSpec):
- #expect_raisesmatches a given String with exception message not exactly but partially. If one needs a partial matching - it can be gained with Regex
- #expect_raisesin general case matches not exception message but result of- #to_scall. It allows to check exception properties other than a message. And it can be gained with a proposed block parameter.
So this way we will get a more flexible matcher and can potentially deprecate #expect_raises in future.
RSpec documentation - https://rspec.info/features/3-13/rspec-expectations/built-in-matchers/raise-error.
Related discussions - #16265