Method/Assertion For Asserting Changes To Add To Your Minitest Arsenal

Posted By Weston Ganger

I was surprised to see that Minitest does not offer an assert_changed and assert_not_changed method by default. Rails has a similar method to this called assert_difference but it does a different job. Here are two ways you can add this to your test suite.

One way to add it, is to get it using my gem rearmed which is a collection of helpful monkey patches for Rails and Ruby that are safely applied on opt-in basis.

Or if you don't use that gem just add this to your test helpers:

@str = 'test'

assert_changed '@str' do
  @str = 'foobar'

assert_not_changed '@str' do
  @str = false

Article Topic:Software Development - Ruby / Rails

Date:December 13, 2016