Update assert_changes and assert_no_changes to use assert_equal

This provides better more specific diffs when comparing complex objects.

Reverts bbe437faec.

Closes #37507. Closes #38220.

Co-authored-by: michdsouza <michd2005@gmail.com>
Co-authored-by: Rahul Purohit <rahulpuroht@gmail.com>
Co-authored-by: Nicholas Koh <nkohwl@gmail.com>
This commit is contained in:
Hrvoje Šimić 2019-11-01 20:25:33 +01:00 committed by Kasper Timm Hansen
parent 0b43409eef
commit 074265a6c1
No known key found for this signature in database
GPG Key ID: 191153215EDA53D8
2 changed files with 37 additions and 12 deletions

View File

@ -177,7 +177,7 @@ module ActiveSupport
retval = assert_nothing_raised(&block)
unless from == UNTRACKED
error = "#{expression.inspect} isn't #{from.inspect}"
error = "Expected change from #{from.inspect}"
error = "#{message}.\n#{error}" if message
assert from === before, error
end
@ -187,12 +187,10 @@ module ActiveSupport
error = "#{expression.inspect} didn't change"
error = "#{error}. It was already #{to}" if before == to
error = "#{message}.\n#{error}" if message
assert before != after, error
assert_not_equal before, after, error
unless to == UNTRACKED
error = "#{expression.inspect} didn't change to as expected\n"
error = "#{error}Expected: #{to.inspect}\n"
error = "#{error} Actual: #{after.inspect}"
error = "Expected change to #{to}\n"
error = "#{message}.\n#{error}" if message
assert to === after, error
end
@ -219,9 +217,14 @@ module ActiveSupport
retval = assert_nothing_raised(&block)
after = exp.call
error = "#{expression.inspect} did change to #{after}"
error = "#{expression.inspect} changed"
error = "#{message}.\n#{error}" if message
assert before == after, error
if before.nil?
assert_nil after, error
else
assert_equal before, after, error
end
retval
end

View File

@ -192,7 +192,7 @@ class AssertionsTest < ActiveSupport::TestCase
@object.increment
end
end
assert_equal "\"@object.num\" isn't nil", error.message
assert_equal "Expected change from nil", error.message
end
def test_assert_changes_with_to_option
@ -208,7 +208,7 @@ class AssertionsTest < ActiveSupport::TestCase
end
end
assert_equal "\"@object.num\" didn't change. It was already 0", error.message
assert_equal "\"@object.num\" didn't change. It was already 0.\nExpected 0 to not be equal to 0.", error.message
end
def test_assert_changes_with_wrong_to_option
@ -272,12 +272,12 @@ class AssertionsTest < ActiveSupport::TestCase
def test_assert_changes_with_message
error = assert_raises Minitest::Assertion do
assert_changes "@object.num", "@object.num should 1", to: 1 do
assert_changes "@object.num", "@object.num should be 1", to: 1 do
@object.decrement
end
end
assert_equal "@object.num should 1.\n\"@object.num\" didn't change to as expected\nExpected: 1\n Actual: -1", error.message
assert_equal "@object.num should be 1.\nExpected change to 1\n", error.message
end
def test_assert_no_changes_pass
@ -293,7 +293,29 @@ class AssertionsTest < ActiveSupport::TestCase
end
end
assert_equal "@object.num should not change.\n\"@object.num\" did change to 1", error.message
assert_equal "@object.num should not change.\n\"@object.num\" changed.\nExpected: 0\n Actual: 1", error.message
end
def test_assert_no_changes_with_long_string_wont_output_everything
lines = "HEY\n" * 12
error = assert_raises Minitest::Assertion do
assert_no_changes "lines" do
lines += "HEY ALSO\n"
end
end
assert_match <<~output, error.message
"lines" changed.
--- expected
+++ actual
@@ -10,4 +10,5 @@
HEY
HEY
HEY
+HEY ALSO
"
output
end
end