Skip to content

Commit 51d73fb

Browse files
committed
Merge pull request rails#37624
Closes rails#37624
2 parents 7113be6 + 074265a commit 51d73fb

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

activesupport/lib/active_support/testing/assertions.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
177177
retval = assert_nothing_raised(&block)
178178

179179
unless from == UNTRACKED
180-
error = "#{expression.inspect} isn't #{from.inspect}"
180+
error = "Expected change from #{from.inspect}"
181181
error = "#{message}.\n#{error}" if message
182182
assert from === before, error
183183
end
@@ -187,12 +187,10 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
187187
error = "#{expression.inspect} didn't change"
188188
error = "#{error}. It was already #{to}" if before == to
189189
error = "#{message}.\n#{error}" if message
190-
assert before != after, error
190+
assert_not_equal before, after, error
191191

192192
unless to == UNTRACKED
193-
error = "#{expression.inspect} didn't change to as expected\n"
194-
error = "#{error}Expected: #{to.inspect}\n"
195-
error = "#{error} Actual: #{after.inspect}"
193+
error = "Expected change to #{to}\n"
196194
error = "#{message}.\n#{error}" if message
197195
assert to === after, error
198196
end
@@ -219,9 +217,14 @@ def assert_no_changes(expression, message = nil, &block)
219217
retval = assert_nothing_raised(&block)
220218
after = exp.call
221219

222-
error = "#{expression.inspect} did change to #{after}"
220+
error = "#{expression.inspect} changed"
223221
error = "#{message}.\n#{error}" if message
224-
assert before == after, error
222+
223+
if before.nil?
224+
assert_nil after, error
225+
else
226+
assert_equal before, after, error
227+
end
225228

226229
retval
227230
end

activesupport/test/test_case_test.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_assert_changes_with_from_option_with_nil
192192
@object.increment
193193
end
194194
end
195-
assert_equal "\"@object.num\" isn't nil", error.message
195+
assert_equal "Expected change from nil", error.message
196196
end
197197

198198
def test_assert_changes_with_to_option
@@ -208,7 +208,7 @@ def test_assert_changes_with_to_option_but_no_change_has_special_message
208208
end
209209
end
210210

211-
assert_equal "\"@object.num\" didn't change. It was already 0", error.message
211+
assert_equal "\"@object.num\" didn't change. It was already 0.\nExpected 0 to not be equal to 0.", error.message
212212
end
213213

214214
def test_assert_changes_with_wrong_to_option
@@ -272,12 +272,12 @@ def test_assert_changes_with_to_and_from_and_case_operator
272272

273273
def test_assert_changes_with_message
274274
error = assert_raises Minitest::Assertion do
275-
assert_changes "@object.num", "@object.num should 1", to: 1 do
275+
assert_changes "@object.num", "@object.num should be 1", to: 1 do
276276
@object.decrement
277277
end
278278
end
279279

280-
assert_equal "@object.num should 1.\n\"@object.num\" didn't change to as expected\nExpected: 1\n Actual: -1", error.message
280+
assert_equal "@object.num should be 1.\nExpected change to 1\n", error.message
281281
end
282282

283283
def test_assert_no_changes_pass
@@ -293,7 +293,29 @@ def test_assert_no_changes_with_message
293293
end
294294
end
295295

296-
assert_equal "@object.num should not change.\n\"@object.num\" did change to 1", error.message
296+
assert_equal "@object.num should not change.\n\"@object.num\" changed.\nExpected: 0\n Actual: 1", error.message
297+
end
298+
299+
def test_assert_no_changes_with_long_string_wont_output_everything
300+
lines = "HEY\n" * 12
301+
302+
error = assert_raises Minitest::Assertion do
303+
assert_no_changes "lines" do
304+
lines += "HEY ALSO\n"
305+
end
306+
end
307+
308+
assert_match <<~output, error.message
309+
"lines" changed.
310+
--- expected
311+
+++ actual
312+
@@ -10,4 +10,5 @@
313+
HEY
314+
HEY
315+
HEY
316+
+HEY ALSO
317+
"
318+
output
297319
end
298320
end
299321

0 commit comments

Comments
 (0)