diff options
Diffstat (limited to 'activemodel/test/cases/validations/presence_validation_test.rb')
-rw-r--r-- | activemodel/test/cases/validations/presence_validation_test.rb | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index 510c13a7c3..ecf16d1e16 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -8,9 +8,9 @@ require 'models/custom_reader' class PresenceValidationTest < ActiveModel::TestCase teardown do - Topic.reset_callbacks(:validate) - Person.reset_callbacks(:validate) - CustomReader.reset_callbacks(:validate) + Topic.clear_validators! + Person.clear_validators! + CustomReader.clear_validators! end def test_validate_presences @@ -41,7 +41,7 @@ class PresenceValidationTest < ActiveModel::TestCase end def test_validates_acceptance_of_with_custom_error_using_quotes - Person.validates_presence_of :karma, :message => "This string contains 'single' and \"double\" quotes" + Person.validates_presence_of :karma, message: "This string contains 'single' and \"double\" quotes" p = Person.new assert p.invalid? assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last @@ -70,4 +70,38 @@ class PresenceValidationTest < ActiveModel::TestCase p[:karma] = "Cold" assert p.valid? end + + def test_validates_presence_of_with_allow_nil_option + Topic.validates_presence_of(:title, allow_nil: true) + + t = Topic.new(title: "something") + assert t.valid?, t.errors.full_messages + + t.title = "" + assert t.invalid? + assert_equal ["can't be blank"], t.errors[:title] + + t.title = " " + assert t.invalid?, t.errors.full_messages + assert_equal ["can't be blank"], t.errors[:title] + + t.title = nil + assert t.valid?, t.errors.full_messages + end + + def test_validates_presence_of_with_allow_blank_option + Topic.validates_presence_of(:title, allow_blank: true) + + t = Topic.new(title: "something") + assert t.valid?, t.errors.full_messages + + t.title = "" + assert t.valid?, t.errors.full_messages + + t.title = " " + assert t.valid?, t.errors.full_messages + + t.title = nil + assert t.valid?, t.errors.full_messages + end end |