Low commit activity in last 3 years
No release in over a year
Call assert_difference with ActiveRecord_Relation objects, arrays or hashes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.0
>= 0
>= 0
 Project Readme

BetterAssertDifference

assert_difference, but better.

Installation

Add this line to your application's Gemfile:

gem 'better_assert_difference'

And then execute:

$ bundle

Or install it yourself as:

$ gem install better_assert_difference

Usage

Include BetterAssertDifference in your tests:

  # For Rails
  ActiveSupport::TestCase.include BetterAssertDifference
  # For MiniTest
  Minitest::Test.include BetterAssertDifference
  # For RSpec
  RSpec.configure do |config|
    config.include AssertDifference
  end

Backward compatible

You can use assert_difference just as usual with string expressions or procs:

  assert_difference('Foo.bar.count', 3) do
    # block omitted
  end

  assert_difference(-> { Foo.bar.count }, 2) do
    # block omitted
  end

  assert_difference([Foo, Bar, Baz], 4) do
    # block omitted
  end

Implicit call of count on ActiveRecord relations

Most of the time you'll want to execute count on ActiveRecord::Relation objects, this call is now implicit and you won't have to use a string expression or a proc.

  assert_difference(Foo, 3) do
    # block omitted
  end
  assert_difference(Foo.where(bar: true)) do
    # block omitted
  end

Multiple differences in one call

Specify an expected difference for each expression:

  assert_difference(Foo.where(bar: true) => 3, Foo.where(baz: false) => 5) do
    # block omitted
  end

Better error message

assert_difference will list all the assertions that have failed, not just the first one.

  2 assertions failed:
  [0] @items.select(&:nil?).count didn't change by 2 {before: 0, after: 1}
  [1] @items.count didn't change by 3 {before: 2, after: 5}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/julien-meichelbeck/better_assert_difference.

License

The gem is available as open source under the terms of the MIT License.