At work recently I’ve been creating the JSON API of our main application, Spreedly Core
and during this time I’ve had a lot of opportunity to try out different testing methods
and start to understand a lot more about the inner workings of both Rspec and Minitest.
One of the interesting things I learned about were Minitest custom assertions.
A custom assertion is exactly what it sounds like, an opportunity to make your own
Minitest assertion if the standard things like assert_equal aren’t doing it for you.
The first thing you’ll need to do is to create a test/custom_assertions.rb file where you’ll
crack open the built in module Minitest::Assertions.
Then, in the test file that you’re working in or your test_helper.rb you can require
your custom_assertions file. From there, making new assertions is easy.
One other nice trick is that you can pass in a custom message to be sent back on failure.
And that’s basically it! Simple and straightforward.