diff options
author | Martin Svalin <martin@lite.nu> | 2011-10-19 18:47:28 +0200 |
---|---|---|
committer | Martin Svalin <martin@lite.nu> | 2011-10-19 21:29:20 +0200 |
commit | c9ca86c29d4f4d8e1181c20ac0da8e1027a14344 (patch) | |
tree | 9c973e42f16bb24ab09504b8bd5f8eddfdb7951f /activemodel/test/cases | |
parent | 89a164ab9b028d855023c88e4afcdf55276a1363 (diff) | |
download | rails-c9ca86c29d4f4d8e1181c20ac0da8e1027a14344.tar.gz rails-c9ca86c29d4f4d8e1181c20ac0da8e1027a14344.tar.bz2 rails-c9ca86c29d4f4d8e1181c20ac0da8e1027a14344.zip |
New #added? method on ActiveModel::Errors
The #added? method makes it possible to check if a specific error has been added, using the same parameters as for #add.
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 784a2b2709..8ddedb160a 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -80,6 +80,49 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["can not be blank"], person.errors[:name] end + test "added? should be true if that error was added" do + person = Person.new + person.errors.add(:name, "can not be blank") + assert person.errors.added?(:name, "can not be blank") + end + + test "added? should handle when message is a symbol" do + person = Person.new + person.errors.add(:name, :blank) + assert person.errors.added?(:name, :blank) + end + + test "added? should handle when message is a proc" do + person = Person.new + message = Proc.new { "can not be blank" } + person.errors.add(:name, message) + assert person.errors.added?(:name, message) + end + + test "added? should default message to :invalid" do + person = Person.new + person.errors.add(:name, :invalid) + assert person.errors.added?(:name) + end + + test "added? should be true when several errors are present, and we ask for one of them" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "is invalid") + assert person.errors.added?(:name, "can not be blank") + end + + test "added? should be false if no errors are present" do + person = Person.new + assert !person.errors.added?(:name) + end + + test "added? should be false when an error is present, but we check for another error" do + person = Person.new + person.errors.add(:name, "is invalid") + assert !person.errors.added?(:name, "can not be blank") + end + test 'should respond to size' do person = Person.new person.errors.add(:name, "can not be blank") |