diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2018-07-08 16:43:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-08 16:43:19 +0200 |
commit | 79edcaeb6ccd7f95029e36dd1a13369b119edfdf (patch) | |
tree | c4dc62c59c1a937c0ccbe8942204bb60fa24f59c | |
parent | 040eb98c2be6bfb2c50b4ad85e7031f9aa97f15e (diff) | |
parent | 4baad7bec63e5d842e36d9d25c1203900a499f67 (diff) | |
download | rails-79edcaeb6ccd7f95029e36dd1a13369b119edfdf.tar.gz rails-79edcaeb6ccd7f95029e36dd1a13369b119edfdf.tar.bz2 rails-79edcaeb6ccd7f95029e36dd1a13369b119edfdf.zip |
Merge pull request #33318 from lxxxvi/multiple_expressions_in_assert_no_difference
Documentation and some tests for `assert_no_difference`
-rw-r--r-- | activesupport/lib/active_support/testing/assertions.rb | 12 | ||||
-rw-r--r-- | activesupport/test/test_case_test.rb | 16 |
2 files changed, 28 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index 6a56da384f..b27ac7ce99 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -113,11 +113,23 @@ module ActiveSupport # post :create, params: { article: invalid_attributes } # end # + # A lambda can be passed in and evaluated. + # + # assert_no_difference -> { Article.count } do + # post :create, params: { article: invalid_attributes } + # end + # # An error message can be specified. # # assert_no_difference 'Article.count', 'An Article should not be created' do # post :create, params: { article: invalid_attributes } # end + # + # An array of expressions can also be passed in and evaluated. + # + # assert_no_difference [ 'Article.count', -> { Post.count } ] do + # post :create, params: { article: invalid_attributes } + # end def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 19901fad99..8698c66e6d 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -52,6 +52,22 @@ class AssertionsTest < ActiveSupport::TestCase assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message end + def test_assert_no_difference_with_multiple_expressions_pass + another_object = @object.dup + assert_no_difference ["@object.num", -> { another_object.num }] do + # ... + end + end + + def test_assert_no_difference_with_multiple_expressions_fail + another_object = @object.dup + assert_raises(Minitest::Assertion) do + assert_no_difference ["@object.num", -> { another_object.num }], "Another Object Changed" do + another_object.increment + end + end + end + def test_assert_difference assert_difference "@object.num", +1 do @object.increment |