DEV Community

Burdette Lamar
Burdette Lamar

Posted on

Is Minitest::Assertions#assert Ever the Best Choice?

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 only Expected false to be truthy.

Whereas:

  • assert_operator(5, :<=, 4) reports Expected 5 to be <= 4.

I have not been able to come up with an example where assert is the best we can do.

Thoughts?

Latest comments (4)

Collapse
 
hopsoft profile image
Hopsoft

Testing consists of the following.

  1. Run some code
  2. Verify that it did what you expected

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.

Collapse
 
briankephart profile image
Brian Kephart

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.

Collapse
 
burdettelamar profile image
Burdette Lamar

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.

Collapse
 
burdettelamar profile image
Burdette Lamar

Thanks, Brian. That's interesting.