Skip to content

Commit 1b195b3

Browse files
Merge pull request rails#48482 from pcreux/improve-assert_changes-error-messages
Improve error messages of `assert_changes` and `assert_no_changes`
2 parents ea51529 + 2b3a002 commit 1b195b3

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

activesupport/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Improve error messages of `assert_changes` and `assert_no_changes`
2+
3+
`assert_changes` error messages now display objects with `.inspect` to make it easier
4+
to differentiate nil from empty strings, strings from symbols, etc.
5+
`assert_no_changes` error messages now surface the actual value.
6+
7+
*pcreux*
8+
19
* Fix `#to_fs(:human_size)` to correctly work with negative numbers.
210

311
*Earlopain*

activesupport/lib/active_support/testing/assertions.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,20 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
195195
retval = _assert_nothing_raised_or_warn("assert_changes", &block)
196196

197197
unless from == UNTRACKED
198-
error = "Expected change from #{from.inspect}, got #{before}"
198+
error = "Expected change from #{from.inspect}, got #{before.inspect}"
199199
error = "#{message}.\n#{error}" if message
200200
assert from === before, error
201201
end
202202

203203
after = exp.call
204204

205205
error = "#{expression.inspect} didn't change"
206-
error = "#{error}. It was already #{to}" if before == to
206+
error = "#{error}. It was already #{to.inspect}" if before == to
207207
error = "#{message}.\n#{error}" if message
208208
refute_equal before, after, error
209209

210210
unless to == UNTRACKED
211-
error = "Expected change to #{to}, got #{after}\n"
211+
error = "Expected change to #{to.inspect}, got #{after.inspect}\n"
212212
error = "#{message}.\n#{error}" if message
213213
assert to === after, error
214214
end
@@ -242,7 +242,7 @@ def assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
242242
retval = _assert_nothing_raised_or_warn("assert_no_changes", &block)
243243

244244
unless from == UNTRACKED
245-
error = "Expected initial value of #{from.inspect}"
245+
error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
246246
error = "#{message}.\n#{error}" if message
247247
assert from === before, error
248248
end

activesupport/test/test_case_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_assert_no_changes_with_from_option_with_nil
331331
@object.increment
332332
end
333333
end
334-
assert_equal "Expected initial value of nil", error.message
334+
assert_equal "Expected initial value of nil, got 0", error.message
335335
end
336336

337337
def test_assert_no_changes_with_from_and_case_operator

0 commit comments

Comments
 (0)