Include amount changed by in `assert_difference` failure message

Unless you're very good at math, this test fail message is not the easiest to debug:

```
"User.count" didn't change by 32.
Expected: 1611
  Actual: 1579
```

It's not obvious from the error, but in this case, it actually changed by 0. This is a pretty strong clue as to what went wrong, but the message doesn't tell us that.

This PR improves the message to make debugging easier:

```
"User.count" didn't change by 32 (changed by 0).
Expected: 1611
  Actual: 1579
```
This commit is contained in:
Alex Ghiculescu 2022-12-30 16:50:31 -06:00
parent 3d00c8b97f
commit 293349c959
3 changed files with 38 additions and 5 deletions

View File

@ -1,3 +1,25 @@
* `assert_difference` message now includes what changed.
This makes it easier to debug non-obvious failures.
Before:
```
"User.count" didn't change by 32.
Expected: 1611
Actual: 1579
```
After:
```
"User.count" didn't change by 32, but by 0.
Expected: 1611
Actual: 1579
```
*Alex Ghiculescu*
* Add ability to match exception messages to `assert_raises` assertion
Instead of this

View File

@ -115,9 +115,10 @@ module ActiveSupport
retval = _assert_nothing_raised_or_warn("assert_difference", &block)
expressions.zip(exps, before) do |(code, diff), exp, before_value|
error = "#{code.inspect} didn't change by #{diff}"
actual = exp.call
error = "#{code.inspect} didn't change by #{diff}, but by #{actual - before_value}"
error = "#{message}.\n#{error}" if message
assert_equal(before_value + diff, exp.call, error)
assert_equal(before_value + diff, actual, error)
end
retval

View File

@ -54,7 +54,7 @@ class AssertionsTest < ActiveSupport::TestCase
@object.increment
end
end
assert_equal "\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message
assert_equal "\"@object.num\" didn't change by 0, but by 1.\nExpected: 0\n Actual: 1", error.message
end
def test_assert_no_difference_with_message_fail
@ -63,7 +63,7 @@ class AssertionsTest < ActiveSupport::TestCase
@object.increment
end
end
assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message
assert_equal "Object Changed.\n\"@object.num\" didn't change by 0, but by 1.\nExpected: 0\n Actual: 1", error.message
end
def test_assert_no_difference_with_multiple_expressions_pass
@ -157,7 +157,17 @@ class AssertionsTest < ActiveSupport::TestCase
@object.increment
end
end
assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message
assert_equal "Object Changed.\n\"@object.num\" didn't change by 0, but by 1.\nExpected: 0\n Actual: 1", error.message
end
def test_assert_difference_message_includes_change
error = assert_raises Minitest::Assertion do
assert_difference "@object.num", +5 do
@object.increment
@object.increment
end
end
assert_equal "\"@object.num\" didn't change by 5, but by 2.\nExpected: 5\n Actual: 2", error.message
end
def test_hash_of_lambda_expressions