You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just realized that negated predicate matchers are not strict, i.e. expect(something).not_to be_foo will pass if foo? returns nil. We have a bunch of these mistakes in rubocop and in rubocop-ast.
I believe it should fail. To be correct, methods ending with ? should typically not return nil. It's quite easy to return nil without wanting to. It's also easy to not realize a method if returning nil (e.g. if x.foo?), but sometimes the nil and false might be treated differently (often without meaning too) and subtle bugs can be introduced.
It may be advisable for compatibility's sake to put that under a flag. I believe that flag should default to strict although that may have to wait for a major version (if ever?).
Note that the only other choice given to rspec user is quite ugly, in particular:
# currently not strict enough version
it_is_expected.not_to be_foo
# correct version
expect(subject.foo?).to be false
Your environment
rspec-expectations version: 3.13.3
The text was updated successfully, but these errors were encountered:
Can you double check you've turned on the existing strict predicate check?
e.g RSpec::Expectations.configuration.strict_predicate_matchers = false
As a quick check shows this to be working as expected for me, we have specs covering it by implementation and adding an exact one passes:
it "fails when #sym? returns nil" do
actual = double("actual", :foo? => nil)
expect {
expect(actual).not_to be_foo
}.to fail_with("expected `#{actual.inspect}.foo?` to return false, got nil")
end
Negated Predicate Matchers should be strict.
I just realized that negated predicate matchers are not strict, i.e.
expect(something).not_to be_foo
will pass iffoo?
returnsnil
. We have a bunch of these mistakes inrubocop
and inrubocop-ast
.I believe it should fail. To be correct, methods ending with
?
should typically not returnnil
. It's quite easy to returnnil
without wanting to. It's also easy to not realize a method if returningnil
(e.g.if x.foo?
), but sometimes thenil
andfalse
might be treated differently (often without meaning too) and subtle bugs can be introduced.It may be advisable for compatibility's sake to put that under a flag. I believe that flag should default to strict although that may have to wait for a major version (if ever?).
Note that the only other choice given to rspec user is quite ugly, in particular:
Your environment
The text was updated successfully, but these errors were encountered: