A nice assert_difference method similar to the one provided by Rails but with some improvements. For example:
assert_difference"Company.count"=>1,"User.count"=>5,"Slot.count"=> -1dopost:somethingendwill assert that a company and 5 users were create (the plus sign is only for the visual aid) and a slot was removed.
Rails' assert_difference would require a more verbose syntax:
assert_difference"Company.count"doassert_difference"User.count",5doassert_difference"Article.count", -1dopost:somethingendendendExpectations can also be ranges, for example:
assert_difference"Blog.count"=>1,"Post.count"=>2..5do# Generate some sample posts when creating a blogpost:createendOn top of that, error reporting is improved by displaying all the counters that didn't match.
To use it with Test::Unit add this code:
classTest::Unit::TestCaseincludeAssertDifferenceendor in Rails:
classActiveSupport::TestCase# ...includeAssertDifferenceendand to use it with RSpec:
RSpec.configuredo |config|
config.includeAssertDifferenceendThis gem requires activesupport >= 3.0.0. It is currently being tested against Ruby 2.2, 2.3, 2.4 and 2.5 with Rails 4.2, 5.0, 5.1 and 5.2, but it's likely to work fine with earlier and later versions of both Ruby and Rails (hence the very permissive dependency on activesupport).
Up to version ~> 1.0.0 of this gem, it was tested with active support 3.0, 3.1, 3.2, 4.0 and 4.1 as well as Ruby 1.9.3, 2.0 and 2.1.
This gem is being used by:
- Modernization of the gem.
- Test with 100% code coverage.
- Started using Travis-CI for continuous testing.
- Started testing in various versions of active support and ruby.
- Improved documentation.
- Expectations can be ranges.
- Fixed important typo.
- Better error reporting (all unmatching counters).
- Fix crash on missing gems.
- Return what the blocks returns.
- Fixed documentation.
- Better organization of code with modules.
- Switched to bundler from jeweler.
- Removed unneeded dependencies.
- Cleaned up documentation.
- Initial release. Code extracted from a personal project.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am "Add some feature") - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Copyright © 2010-2018 José Pablo Fernández. See LICENSE for details.



