RSpec, predicates, and be_ matchers
Reported by Rick DeNatale | January 10th, 2008 @ 04:32 AM | in Second Edition
Page 599 introduces expectations against predicates in RSpec
@credit_card.valid? should == true is given as an alternative to @credit_card.should be_value
There's a subtlety missed here in that this assumes that the valid? method actually returns true when it 'succeeds.' But in Ruby it's not uncommon for predicates to take advantage of the fact that in almost all boolean uses (e.g. in an if statement) anything other than nil or false is considered to be true. I'd call this being 'true' in the Ruby sense.
So checking for equality with true isn't a best practice IMHO.
The 'magic' predicate matchers like be_value set an expectation that the value will be 'true' in the Ruby sense.
Also on the top of the next page there's is a list of the 'simplest' hard-coded predicate style matchers. There's actually a simpler one on the list which is just be
which sets an expectation that the result being matched is 'true' in the Ruby sense.
So to specify that a value would say cause the true leg of an if test to be taken one writes:
expression.should be
Comments and changes to this ticket
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
