[rspec-users] RSpec style and truthiness
dchelimsky at gmail.com
Thu Mar 19 20:37:13 EDT 2009
2009/3/19 Rick DeNatale <rick.denatale at gmail.com>:
> I like to avoid over-constraining specifications, so for example of methods
> which return 'boolean' values, I prefer to test either truthiness (anything
> but false or nil), or falsiness (either false or nil).
> This isn't an issue true predicate methods which are of the right form to
> use be_whatever (as in a whatever? method taking no arguments). But for a
> method taking arguments this doesn't work (I think!), and even if it did I
> wouldn't want to use it for some cases. For instance I'm working with some
> code now (which I'm not really free to change) which has methods like
> has_member?(user) which tells whether or not the object has user as a
> member. So I tend to write things like
> @it.has_member?(user).should be
You *can* actually say @it.should have_member(user) :)
> when the response is expected to be truthy
> @it.has_member?(user).should_not be
> Now I recently noticed that if the second expectation fails, I get a rather
> snarky message like 'not only did it fail, but it's awkwardly expressed, try
> to express it as a positive.'
Pardon the snarkiness (I wrote that) - that was intended for a
different situation (should_not be < 5, instead of should be >= 5),
and the fact that you're seeing it for just 'be' is a bug.
> While I agree that it isn't as elegant an expression as I like, I don't see
> a way out of the box to check that a result should be a falsy (or should
> that be non-truthy) value.
> Even 'should be' is a bit grating. I'm tempted to write a pair of matchers
> like be_truthy and be_falsy, but I was wondering what other RSpec users have
> to say.
I can't tell you why, but be_truthy sounds fine to me, but be_falsy
How about "should be_truthy" or "should_not be_truthy"?
> Rick DeNatale
> Blog: http://talklikeaduck.denhaven2.com/
> Twitter: http://twitter.com/RickDeNatale
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn: http://www.linkedin.com/in/rickdenatale
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users