I'm writing something about Minitest::Assertions
, which causes me to wonder:
- Is there an occasion when a simple
assert(test)
could not be replaced by a more informative assertion?
For example:
-
assert(5 <= 4)
reports onlyExpected false to be truthy.
Whereas:
-
assert_operator(5, :<=, 4)
reportsExpected 5 to be <= 4.
I have not been able to come up with an example where assert
is the best we can do.
Thoughts?
Top comments (4)
I actually tend to think the opposite, that the other assertions are just sugared-up
assert
. Sure, the default message isn't informative, but the others aren't any better IMO.Expected 5 to be <= 4
doesn't tell you anything about what those numbers mean or why they're in your tests. I almost always customize the error message to be informative in the context of my own code. Since I'm doing that anyway, it saves headspace not to try and remember alternative constructions, especially when I can never remember which assertions are from Minitest, which are from Rails, and which have been deprecated/removed.The place where I use Minitest::Assertions is in developing my own libraries. In that work I allow no bug to live more than a day, so all I really need is the expected/actual values and the line number. And the assertions give me those.
For other work (automated testing for legacy software, for example), I use different toolsets altogether.
Thanks, Brian. That's interesting.
Testing consists of the following.
A simple assert embodies step 2.
Everything else is syntactic sugar that introduces complexity and brittleness to your project. It's true that simple assert messages can be obtuse, but this is easily addressed with custom messages as Brian recommended. Also, odds are pretty good that you'll be adding a breakpoint and debugging the failure anyway.