I used to write my assertions like this:
assert user.active?
refute user.inactive?
Then I joined a team where I was encouraged to write this:
assert_equal true, user.active?
assert_equal false, user.inactive?
What? That doesn’t look very nice. That’s doesn’t feel very Ruby. It’s less elegant!
Here’s the thing, though: your unit tests aren’t about being elegant. They’re about guaranteeing correctness. You open the door to some insidious bugs when you test truthiness instead of truth.
- You might perform an assignment rather than comparing equality.
def active?
# Should be status == :active
status = :active
end
- You might check the presence of an array, rather than its length.
def has_users?
# Should be user_list.any?
user_list
end
def user_list
[]
end
Over time, I’ve gotten used to it. This style still chafes, but not as badly as bugs caused by returning the wrong value from a predicate method.
Top comments (1)
It's a good idea, because in a lot of Ruby things only have to be "Boolean-like" rather than actually true and false.
In fact there's been a bit of an ongoing debate in the Rails codebase between the two and it can be quite confusing, as one in-particular that presents as Boolean by name can return
0
. While that's truthy in Ruby it still creates confusion, even if it is an index position, because of the way the method is presented as boolean.