aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorJulien Meichelbeck <julien.meichelbeck@gmail.com>2018-01-18 21:20:34 +0100
committerRafael França <rafaelmfranca@gmail.com>2018-01-18 15:20:34 -0500
commite0f0d717d63b7896186f40c4d389977ddba72a97 (patch)
treeb972eb23e30388aa087cddae7559252b03647a14 /activesupport/test
parentccfc2d63cade1d68de98c8051fd63c2348c6c914 (diff)
downloadrails-e0f0d717d63b7896186f40c4d389977ddba72a97.tar.gz
rails-e0f0d717d63b7896186f40c4d389977ddba72a97.tar.bz2
rails-e0f0d717d63b7896186f40c4d389977ddba72a97.zip
Support hash as first argument in `assert_difference`. (#31600)
* Support hash as first argument for `assert_difference`. This allows to specify multiple numeric differences in the same assertion. Example: assert_difference 'Article.count' => 1, 'Notification.count' => 2 do # post :create, params: { article: {...} } end * Support error message when passing a hash as a first parameter * Format CHANGELOG properly [Julien Meichelbeck + Rafael Mendonça França]
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/test_case_test.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb
index 79b75a001a..eced622137 100644
--- a/activesupport/test/test_case_test.rb
+++ b/activesupport/test/test_case_test.rb
@@ -115,6 +115,35 @@ class AssertDifferenceTest < ActiveSupport::TestCase
end
end
+ def test_hash_of_expressions
+ assert_difference "@object.num" => 1, "@object.num + 1" => 1 do
+ @object.increment
+ end
+ end
+
+ def test_hash_of_expressions_with_message
+ error = assert_raises Minitest::Assertion do
+ assert_difference({ "@object.num" => 0 }, "Object Changed") do
+ @object.increment
+ end
+ end
+ assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message
+ end
+
+ def test_hash_of_lambda_expressions
+ assert_difference -> { @object.num } => 1, -> { @object.num + 1 } => 1 do
+ @object.increment
+ end
+ end
+
+ def test_hash_of_expressions_identify_failure
+ assert_raises(Minitest::Assertion) do
+ assert_difference "@object.num" => 1, "1 + 1" => 1 do
+ @object.increment
+ end
+ end
+ end
+
def test_assert_changes_pass
assert_changes "@object.num" do
@object.increment