diff options
Diffstat (limited to 'activemodel/test/cases/errors_test.rb')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 43aee5a814..fe351f922d 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "cases/helper" require "active_support/core_ext/string/strip" require "yaml" @@ -81,7 +83,7 @@ class ErrorsTest < ActiveModel::TestCase assert_equal 1, person.errors.count person.errors.clear - assert person.errors.empty? + assert_empty person.errors end test "error access is indifferent" do @@ -126,8 +128,8 @@ class ErrorsTest < ActiveModel::TestCase test "detecting whether there are errors with empty?, blank?, include?" do person = Person.new person.errors[:foo] - assert person.errors.empty? - assert person.errors.blank? + assert_empty person.errors + assert_predicate person.errors, :blank? assert_not_includes person.errors, :foo end @@ -221,6 +223,13 @@ class ErrorsTest < ActiveModel::TestCase assert !person.errors.added?(:name) end + test "added? returns false when checking for an error by symbol and a different error with same message is present" do + I18n.backend.store_translations("en", errors: { attributes: { name: { wrong: "is wrong", used: "is wrong" } } }) + person = Person.new + person.errors.add(:name, :wrong) + assert !person.errors.added?(:name, :used) + end + test "size calculates the number of error messages" do person = Person.new person.errors.add(:name, "cannot be blank") @@ -311,7 +320,7 @@ class ErrorsTest < ActiveModel::TestCase test "generate_message works without i18n_scope" do person = Person.new - assert !Person.respond_to?(:i18n_scope) + assert_not_respond_to Person, :i18n_scope assert_nothing_raised { person.errors.generate_message(:name, :blank) } @@ -362,7 +371,7 @@ class ErrorsTest < ActiveModel::TestCase assert_equal 1, person.errors.details.count person.errors.clear - assert person.errors.details.empty? + assert_empty person.errors.details end test "copy errors" do @@ -375,6 +384,18 @@ class ErrorsTest < ActiveModel::TestCase assert_equal [:name], person.errors.details.keys end + test "merge errors" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :invalid) + + person = Person.new + person.errors.add(:name, :blank) + person.errors.merge!(errors) + + assert_equal({ name: ["can't be blank", "is invalid"] }, person.errors.messages) + assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details) + end + test "errors are marshalable" do errors = ActiveModel::Errors.new(Person.new) errors.add(:name, :invalid) |