From 074265a6c19b8f629e3211405e940b069ccd5667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Fri, 1 Nov 2019 20:25:33 +0100 Subject: [PATCH] Update assert_changes and assert_no_changes to use assert_equal This provides better more specific diffs when comparing complex objects. Reverts bbe437faecca5fd6bdc2327a4bc7a31ba21afe2e. Closes #37507. Closes #38220. Co-authored-by: michdsouza Co-authored-by: Rahul Purohit Co-authored-by: Nicholas Koh --- .../lib/active_support/testing/assertions.rb | 17 ++++++---- activesupport/test/test_case_test.rb | 32 ++++++++++++++++--- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index f283b987f3..7ff9fd0532 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -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 diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index dd75548e9c..cc5c50c6a0 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -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