Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cop idea: use with_message matcher instead of matching error message manually #1255

Open
Darhazer opened this issue Mar 25, 2022 · 2 comments
Labels

Comments

@Darhazer
Copy link
Member

I often see checks on message expectations like

expect(subject.foo).to raise_error(Error) do |error|
  expect(error.message).to match(/..../)
end

while raise_error offers .with_message

@pirj
Copy link
Member

pirj commented Mar 25, 2022

I'm not certain why .with_message is specifically needed, as raise_error itself accepts a second argument for message matching:

 expect { raise "oops" }.to raise_error(RuntimeError, /op/)

It might be failure messages are slightly different, needs to be checked.

@Darhazer Darhazer added the cop label Mar 30, 2022
@tejasbubane
Copy link
Contributor

@pirj Both .with_message and raise_error's second argument give exactly same errors, I'm not sure which one to prefer for this cop:

Failures:

  1) Errors runs raise_error with block
     Failure/Error:
       expect { 1 }.to raise_error(StandardError) do |error|
         expect(error.message).to eq('foo')
       end
     
       expected StandardError but nothing was raised
     # ./raise_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Errors runs raise_error.with_message string
     Failure/Error: expect { 1 }.to raise_error(StandardError).with_message('foo')
       expected StandardError with "foo" but nothing was raised
     # ./raise_spec.rb:13:in `block (2 levels) in <top (required)>'

  3) Errors runs raise_error.with_message string
     Failure/Error: expect { 1 }.to raise_error(StandardError).with_message(/foo/)
       expected StandardError with message matching /foo/ but nothing was raised
     # ./raise_spec.rb:17:in `block (2 levels) in <top (required)>'

  4) Errors runs raise_error & second argument string
     Failure/Error: expect { 1 }.to raise_error(StandardError, 'foo')
       expected StandardError with "foo" but nothing was raised
     # ./raise_spec.rb:21:in `block (2 levels) in <top (required)>'

  5) Errors runs raise_error & second argument regex
     Failure/Error: expect { 1 }.to raise_error(StandardError, /foo/)
       expected StandardError with message matching /foo/ but nothing was raised
     # ./raise_spec.rb:25:in `block (2 levels) in <top (required)>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants