diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-20 00:57:25 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-20 00:57:25 -0300 |
commit | cc22c9eae3ebfb7505e91ec5a77456ce5d6ada3c (patch) | |
tree | edd9c9807a0c247fb75db410e6b78a0754ce59da /activesupport/test | |
parent | e2b71e9f8eeac691c6063d7d9cb52db1c1210bcb (diff) | |
parent | 16f24cd10ffca6be49e394b9404e9564a94aeeda (diff) | |
download | rails-cc22c9eae3ebfb7505e91ec5a77456ce5d6ada3c.tar.gz rails-cc22c9eae3ebfb7505e91ec5a77456ce5d6ada3c.tar.bz2 rails-cc22c9eae3ebfb7505e91ec5a77456ce5d6ada3c.zip |
Merge pull request #25393 from gsamokovarov/introduce-assert-changes
Introduce `assert_changes` and `assert_no_changes`
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/test_case_test.rb | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 18228a2ac5..772c3cfca7 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -111,6 +111,112 @@ class AssertDifferenceTest < ActiveSupport::TestCase end end end + + def test_assert_changes_pass + assert_changes '@object.num' do + @object.increment + end + end + + def test_assert_changes_pass_with_lambda + assert_changes -> { @object.num } do + @object.increment + end + end + + def test_assert_changes_with_from_option + assert_changes '@object.num', from: 0 do + @object.increment + end + end + + def test_assert_changes_with_from_option_with_wrong_value + assert_raises Minitest::Assertion do + assert_changes '@object.num', from: -1 do + @object.increment + end + end + end + + def test_assert_changes_with_to_option + assert_changes '@object.num', to: 1 do + @object.increment + end + end + + def test_assert_changes_with_wrong_to_option + assert_raises Minitest::Assertion do + assert_changes '@object.num', to: 2 do + @object.increment + end + end + end + + def test_assert_changes_with_from_option_and_to_option + assert_changes '@object.num', from: 0, to: 1 do + @object.increment + end + end + + def test_assert_changes_with_from_and_to_options_and_wrong_to_value + assert_raises Minitest::Assertion do + assert_changes '@object.num', from: 0, to: 2 do + @object.increment + end + end + end + + def test_assert_changes_works_with_any_object + retval = silence_warnings do + assert_changes :@new_object, from: nil, to: 42 do + @new_object = 42 + end + end + + assert_equal 42, retval + end + + def test_assert_changes_works_with_nil + oldval = @object + + retval = assert_changes :@object, from: oldval, to: nil do + @object = nil + end + + assert_nil retval + end + + def test_assert_changes_with_to_and_case_operator + token = nil + + assert_changes 'token', to: /\w{32}/ do + token = SecureRandom.hex + end + end + + def test_assert_changes_with_to_and_from_and_case_operator + token = SecureRandom.hex + + assert_changes 'token', from: /\w{32}/, to: /\w{32}/ do + token = SecureRandom.hex + end + end + + def test_assert_no_changes_pass + assert_no_changes '@object.num' do + # ... + end + end + + def test_assert_no_changes_with_message + error = assert_raises Minitest::Assertion do + assert_no_changes '@object.num', '@object.num should not change' do + @object.increment + end + end + + assert_equal "@object.num should not change.\n\"@object.num\" did change to 1.\nExpected: 0\n Actual: 1", error.message + end end class AlsoDoingNothingTest < ActiveSupport::TestCase |