Skip to content
Permalink
Browse files

Update assert_changes and assert_no_changes to use assert_equal

This provides better more specific diffs when comparing complex objects.

Reverts bbe437f.

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>
  • Loading branch information
4 people authored and kaspth committed Nov 1, 2019
1 parent 0b43409 commit 074265a6c19b8f629e3211405e940b069ccd5667
Showing with 37 additions and 12 deletions.
  1. +10 −7 activesupport/lib/active_support/testing/assertions.rb
  2. +27 −5 activesupport/test/test_case_test.rb
@@ -177,7 +177,7 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
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 @@ def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &b
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 @@ def assert_no_changes(expression, message = nil, &block)
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
@@ -192,7 +192,7 @@ def test_assert_changes_with_from_option_with_nil
@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 @@ def test_assert_changes_with_to_option_but_no_change_has_special_message
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 @@ def test_assert_changes_with_to_and_from_and_case_operator

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 @@ def test_assert_no_changes_with_message
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

0 comments on commit 074265a

Please sign in to comment.
You can’t perform that action at this time.