Why is negative specific expectation of an exception deprecated in RSpec?
It looks like I am allowed to specify the exception class for .to
, but not for .not_to
?
What are the exact reasons for this?
Failure/Error: expect{ smth }.not_to raise_exception SomeExceptionClass
ArgumentError:
`expect { }.not_to raise_error(SpecificErrorClass)` is not valid, use `expect { }.not_to raise_error` (with no args) instead
To deploy on Nakilon a bit:
This is a constructive decision on their part. They seem to think that this is not a very good test, because if you expect some bug not to be escalated, then the test will pass if:
- no error occurs.
- some other error occurs.
... which is at least inaccurate. Your code probably only wants to do one of these things.
It looks like this is reasoning, I would not want to say how true it is.
From the changelog :
Make
expect { }.to_not raise_error(SomeSpecificClass, message)
,expect { }.to_not raise_error(SomeSpecificClass)
andexpect { }.to_not raise_error(message)
invalid, as they are prone to obfuscation errors. Useexpect { }.to_not raise_error
(no arguments) instead . (Sam Phippen)
It looks like they figured out that there is no reason to drop the exception class parameter, so it currently works: https://www.relishapp.com/rspec/rspec-expectations/v/3-5/docs/built-in- matchers / raise-error-matcher
For example, in Selenium, checking that some condition becomes true:
Selenium::WebDriver::Wait.new(
message: message,
timeout: timeout,
).until do
f() == x
end
it is now easy to wait for it not to be false in the following seconds, negating the timeout exception:
begin
Selenium::WebDriver::Wait.new(
timeout: timeout,
).until do
f() != x
end
raise message
rescue Selenium::WebDriver::Error::TimeOutError
end