Skip to content

Commit

Permalink
Merge pull request #46863 from ghiculescu/assert-diffrence-message
Browse files Browse the repository at this point in the history
Include amount changed by in `assert_difference` failure message
  • Loading branch information
byroot committed Jan 7, 2023
2 parents 15a0325 + 293349c commit 05acc18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
22 changes: 22 additions & 0 deletions activesupport/CHANGELOG.md
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions activesupport/lib/active_support/testing/assertions.rb
Expand Up @@ -116,9 +116,10 @@ def assert_difference(expression, *args, &block)
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
Expand Down
16 changes: 13 additions & 3 deletions activesupport/test/test_case_test.rb
Expand Up @@ -54,7 +54,7 @@ def test_assert_no_difference_fail
@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
Expand All @@ -63,7 +63,7 @@ def test_assert_no_difference_with_message_fail
@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
Expand Down Expand Up @@ -157,7 +157,17 @@ def test_hash_of_expressions_with_message
@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
Expand Down

0 comments on commit 05acc18

Please sign in to comment.