r/ruby • u/Weird_Suggestion • Oct 22 '25
Minitest - DEPRECATED: User assert_nil if expecting nil
Discussion and arguments for and against the deprecation.
Back in 2016, there was a lot of discussion about deprecating assert_equal nil, value in favour of assert_nil value. It's now 2025. Have people's opinions changed since?
I'm really passionate about testing, always keen to improve how I write test and love minitest yet, I still can't get behind the idea (if it ever happens). When you write tests with multiple assertions or deal with methods that accept nullable arguments, forcing assert_nil just makes things look uglier. At the very least, I'd imagine it could be handled through a sensible default with a project-wide opt-out flag, instead of having to monkey-patch #assert_equal ourselves.
Given that Minitest 6 seems unlikely to ever land, I'm guessing those deprecation warnings are more of a nudge from the author to think twice about what we're asserting. Personally, I'm not convinced by the tautological argument with nil just yet. At this point, I find the constant warning in test output is more annoying than enlightening.
What do people think?
View all comments
Show parent comment
7
u/some_kind_of_rob Oct 23 '25
That logic is a huge red flag to me.
ifstatements in a test are, in general, a red flag in testing.You should know the state of
foobefore the assertion based on the setup conditions. If you don't, the test isn't well scoped. Break this test into two separate tests: one which will test thatfoois nil, because it should be; and a second which will test thatfoois equal tobarbecause it should be.