Today I Learned

A Hashrocket project

🧮 assert_difference with negative values

ActiveSupport::Testing::Assertions can check that a value was reduced because of a block:

test 'should destroy' do
  assert_difference('User.count', -1) do
    delete :destroy, id: user.id
  end
end

#assert_difference has a default value of 1 so negative numbers must be explicit.

*https://api.rubyonrails.org/v5.0/classes/ActiveSupport/Testing/Assertions.html#method-i-assert_difference

Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. Contact us and find out how we can help you.