RSpec be_truthy, exist, or be true?
The documentation for RSpec is amazingly detailed, but sometimes falls a bit short on when to apply certain matchers. In many of the specs that I’ve reviewed, incorrect matchers were used to test for pure Boolean values.
be_truthy and be_falsey
expect(some_evaluation).to be_truthy expect(some_evaluation).to be_falsey
be_truthy should only be used when expecting an evaluation to be not
nil and not
be_falsey should only be used when expecting an evaluation to be
be_truthy does not actually check for the Boolean value
If you have a method that should only be evaluated into a Boolean, do not use these matchers for your spec.
be true and be false
expect(some_evaluation).to be true expect(some_evaluation).to be false
be true and
be false expect only
false returned, respectively. This is a straight
Continue reading →