diff options
author | Roberto Vasquez Angel <robotex@robotex.de> | 2012-07-25 17:07:05 +0200 |
---|---|---|
committer | Steve Klabnik <steve@steveklabnik.com> | 2012-12-15 16:27:44 -0500 |
commit | d72a07f1d1478db9daed847eadb35bfd840674f6 (patch) | |
tree | a48970e3b40c23c23e52249f2a7a61f85fcf94d1 /activemodel/test | |
parent | c298ee403d60db6b082539c8dd4f95754034274d (diff) | |
download | rails-d72a07f1d1478db9daed847eadb35bfd840674f6.tar.gz rails-d72a07f1d1478db9daed847eadb35bfd840674f6.tar.bz2 rails-d72a07f1d1478db9daed847eadb35bfd840674f6.zip |
Add `ActiveModel::Validations::AbsenceValidator`, a validator to check the absence of attributes.
Add `ActiveModel::Errors#add_on_present` method. Adds error messages to present attributes.
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/validations/absence_validation_test.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/absence_validation_test.rb b/activemodel/test/cases/validations/absence_validation_test.rb new file mode 100644 index 0000000000..c05d71de5a --- /dev/null +++ b/activemodel/test/cases/validations/absence_validation_test.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/topic' +require 'models/person' +require 'models/custom_reader' + +class AbsenceValidationTest < ActiveModel::TestCase + teardown do + Topic.reset_callbacks(:validate) + Person.reset_callbacks(:validate) + CustomReader.reset_callbacks(:validate) + end + + def test_validate_absences + Topic.validates_absence_of(:title, :content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + t.title = "" + t.content = "something" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:content] + t.content = "" + assert t.valid? + end + + def test_accepts_array_arguments + Topic.validates_absence_of %w(title content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + end + + def test_validates_acceptance_of_with_custom_error_using_quotes + Person.validates_absence_of :karma, message: "This string contains 'single' and \"double\" quotes" + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last + end + + def test_validates_absence_of_for_ruby_class + Person.validates_absence_of :karma + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p.karma = nil + assert p.valid? + end + + def test_validates_absence_of_for_ruby_class_with_custom_reader + CustomReader.validates_absence_of :karma + p = CustomReader.new + p[:karma] = "excellent" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p[:karma] = "" + assert p.valid? + end +end |