As I discovered today, assert_not_equal has a pretty big problem: it does exactly what you ask it to do.
I just need to make sure that a user has at least one record:
assert_not_equal 0, UserPreference.find_by_user_id(user_id)
As you may have just caught: that is always going to pass. Not a tough mistake when you’re motoring along and not putting too much thought into something so simple: The finder isn’t going to return the count of matching rows… it’s either going to return a populated object or nil, neither of which will be equal to 0.
Fixing the bug is trivial, but lesson learned:
From now on whenever I write assert_not_equal – I am going to make sure it works by temporarily changing it to assert_equal and taking a look at the error message.
In this case: if the error message said something like “<0> expected but was <2>”, I would know that things are working.
Whereas if the error said “<0> expected but was <#<UserPreference…>>”, I’d know right away that I need to fix it up (and have one more cup of coffee).