Improve error messages of assert_changes and assert_no_changes

This commit is contained in:
Philippe Creux 2023-06-15 11:07:31 +02:00 committed by Jonathan Hefner
parent 4425ba2017
commit 2b3a002a2a
3 changed files with 13 additions and 5 deletions

@ -1,3 +1,11 @@
* Improve error messages of `assert_changes` and `assert_no_changes`
`assert_changes` error messages now display objects with `.inspect` to make it easier
to differentiate nil from empty strings, strings from symbols, etc.
`assert_no_changes` error messages now surface the actual value.
*pcreux*
* Fix `#to_fs(:human_size)` to correctly work with negative numbers.
*Earlopain*

@ -195,7 +195,7 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
retval = _assert_nothing_raised_or_warn("assert_changes", &block)
unless from == UNTRACKED
error = "Expected change from #{from.inspect}, got #{before}"
error = "Expected change from #{from.inspect}, got #{before.inspect}"
error = "#{message}.\n#{error}" if message
assert from === before, error
end
@ -203,12 +203,12 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
after = exp.call
error = "#{expression.inspect} didn't change"
error = "#{error}. It was already #{to}" if before == to
error = "#{error}. It was already #{to.inspect}" if before == to
error = "#{message}.\n#{error}" if message
refute_equal before, after, error
unless to == UNTRACKED
error = "Expected change to #{to}, got #{after}\n"
error = "Expected change to #{to.inspect}, got #{after.inspect}\n"
error = "#{message}.\n#{error}" if message
assert to === after, error
end
@ -242,7 +242,7 @@ def assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
retval = _assert_nothing_raised_or_warn("assert_no_changes", &block)
unless from == UNTRACKED
error = "Expected initial value of #{from.inspect}"
error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
error = "#{message}.\n#{error}" if message
assert from === before, error
end

@ -331,7 +331,7 @@ def test_assert_no_changes_with_from_option_with_nil
@object.increment
end
end
assert_equal "Expected initial value of nil", error.message
assert_equal "Expected initial value of nil, got 0", error.message
end
def test_assert_no_changes_with_from_and_case_operator