aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2019-04-24 16:16:00 -0400
committerGitHub <noreply@github.com>2019-04-24 16:16:00 -0400
commitd4d145a6795ee7f461ef86a07e73a1f13fdb8574 (patch)
treeed4780a32c4e78d9890db43e02499e1b2e25d907 /activemodel/test
parent9834be65655e8552d25633b7376ab0654a23875d (diff)
parent5e24c333505c3bab3c85d834ac985281f141709f (diff)
downloadrails-d4d145a6795ee7f461ef86a07e73a1f13fdb8574.tar.gz
rails-d4d145a6795ee7f461ef86a07e73a1f13fdb8574.tar.bz2
rails-d4d145a6795ee7f461ef86a07e73a1f13fdb8574.zip
Merge pull request #32313 from lulalala/model_error_as_object
Model error as object
Diffstat (limited to 'activemodel/test')
-rw-r--r--activemodel/test/cases/error_test.rb200
-rw-r--r--activemodel/test/cases/errors_test.rb292
-rw-r--r--activemodel/test/cases/nested_error_test.rb54
-rw-r--r--activemodel/test/cases/validations/i18n_validation_test.rb19
-rw-r--r--activemodel/test/cases/validations/validations_context_test.rb4
-rw-r--r--activemodel/test/cases/validations/with_validation_test.rb8
-rw-r--r--activemodel/test/cases/validations_test.rb14
-rw-r--r--activemodel/test/models/person_with_validator.rb2
-rw-r--r--activemodel/test/models/reply.rb10
-rw-r--r--activemodel/test/validators/email_validator.rb5
10 files changed, 544 insertions, 64 deletions
diff --git a/activemodel/test/cases/error_test.rb b/activemodel/test/cases/error_test.rb
new file mode 100644
index 0000000000..d74321fee5
--- /dev/null
+++ b/activemodel/test/cases/error_test.rb
@@ -0,0 +1,200 @@
+# frozen_string_literal: true
+
+require "cases/helper"
+require "active_model/error"
+
+class ErrorTest < ActiveModel::TestCase
+ class Person
+ extend ActiveModel::Naming
+ def initialize
+ @errors = ActiveModel::Errors.new(self)
+ end
+
+ attr_accessor :name, :age
+ attr_reader :errors
+
+ def read_attribute_for_validation(attr)
+ send(attr)
+ end
+
+ def self.human_attribute_name(attr, options = {})
+ attr
+ end
+
+ def self.lookup_ancestors
+ [self]
+ end
+ end
+
+ def test_initialize
+ base = Person.new
+ error = ActiveModel::Error.new(base, :name, :too_long, foo: :bar)
+ assert_equal base, error.base
+ assert_equal :name, error.attribute
+ assert_equal :too_long, error.type
+ assert_equal({ foo: :bar }, error.options)
+ end
+
+ test "initialize without type" do
+ error = ActiveModel::Error.new(Person.new, :name)
+ assert_equal :invalid, error.type
+ assert_equal({}, error.options)
+ end
+
+ test "initialize without type but with options" do
+ options = { message: "bar" }
+ error = ActiveModel::Error.new(Person.new, :name, options)
+ assert_equal(options, error.options)
+ end
+
+ # match?
+
+ test "match? handles mixed condition" do
+ subject = ActiveModel::Error.new(Person.new, :mineral, :not_enough, count: 2)
+ assert_not subject.match?(:mineral, :too_coarse)
+ assert subject.match?(:mineral, :not_enough)
+ assert subject.match?(:mineral, :not_enough, count: 2)
+ assert_not subject.match?(:mineral, :not_enough, count: 1)
+ end
+
+ test "match? handles attribute match" do
+ subject = ActiveModel::Error.new(Person.new, :mineral, :not_enough, count: 2)
+ assert_not subject.match?(:foo)
+ assert subject.match?(:mineral)
+ end
+
+ test "match? handles error type match" do
+ subject = ActiveModel::Error.new(Person.new, :mineral, :not_enough, count: 2)
+ assert_not subject.match?(:mineral, :too_coarse)
+ assert subject.match?(:mineral, :not_enough)
+ end
+
+ test "match? handles extra options match" do
+ subject = ActiveModel::Error.new(Person.new, :mineral, :not_enough, count: 2)
+ assert_not subject.match?(:mineral, :not_enough, count: 1)
+ assert subject.match?(:mineral, :not_enough, count: 2)
+ end
+
+ # message
+
+ test "message with type as a symbol" do
+ error = ActiveModel::Error.new(Person.new, :name, :blank)
+ assert_equal "can't be blank", error.message
+ end
+
+ test "message with custom interpolation" do
+ subject = ActiveModel::Error.new(Person.new, :name, :inclusion, message: "custom message %{value}", value: "name")
+ assert_equal "custom message name", subject.message
+ end
+
+ test "message returns plural interpolation" do
+ subject = ActiveModel::Error.new(Person.new, :name, :too_long, count: 10)
+ assert_equal "is too long (maximum is 10 characters)", subject.message
+ end
+
+ test "message returns singular interpolation" do
+ subject = ActiveModel::Error.new(Person.new, :name, :too_long, count: 1)
+ assert_equal "is too long (maximum is 1 character)", subject.message
+ end
+
+ test "message returns count interpolation" do
+ subject = ActiveModel::Error.new(Person.new, :name, :too_long, message: "custom message %{count}", count: 10)
+ assert_equal "custom message 10", subject.message
+ end
+
+ test "message handles lambda in messages and option values, and i18n interpolation" do
+ subject = ActiveModel::Error.new(Person.new, :name, :invalid,
+ foo: "foo",
+ bar: "bar",
+ baz: Proc.new { "baz" },
+ message: Proc.new { |model, options|
+ "%{attribute} %{foo} #{options[:bar]} %{baz}"
+ }
+ )
+ assert_equal "name foo bar baz", subject.message
+ end
+
+ test "generate_message works without i18n_scope" do
+ person = Person.new
+ error = ActiveModel::Error.new(person, :name, :blank)
+ assert_not_respond_to Person, :i18n_scope
+ assert_nothing_raised {
+ error.message
+ }
+ end
+
+ test "message with type as custom message" do
+ error = ActiveModel::Error.new(Person.new, :name, message: "cannot be blank")
+ assert_equal "cannot be blank", error.message
+ end
+
+ test "message with options[:message] as custom message" do
+ error = ActiveModel::Error.new(Person.new, :name, :blank, message: "cannot be blank")
+ assert_equal "cannot be blank", error.message
+ end
+
+ test "message renders lazily using current locale" do
+ error = nil
+
+ I18n.backend.store_translations(:pl, errors: { messages: { invalid: "jest nieprawidłowe" } })
+
+ I18n.with_locale(:en) { error = ActiveModel::Error.new(Person.new, :name, :invalid) }
+ I18n.with_locale(:pl) {
+ assert_equal "jest nieprawidłowe", error.message
+ }
+ end
+
+ test "message uses current locale" do
+ I18n.backend.store_translations(:en, errors: { messages: { inadequate: "Inadequate %{attribute} found!" } })
+ error = ActiveModel::Error.new(Person.new, :name, :inadequate)
+ assert_equal "Inadequate name found!", error.message
+ end
+
+ # full_message
+
+ test "full_message returns the given message when attribute is :base" do
+ error = ActiveModel::Error.new(Person.new, :base, message: "press the button")
+ assert_equal "press the button", error.full_message
+ end
+
+ test "full_message returns the given message with the attribute name included" do
+ error = ActiveModel::Error.new(Person.new, :name, :blank)
+ assert_equal "name can't be blank", error.full_message
+ end
+
+ test "full_message uses default format" do
+ error = ActiveModel::Error.new(Person.new, :name, message: "can't be blank")
+
+ # Use a locale without errors.format
+ I18n.with_locale(:unknown) {
+ assert_equal "name can't be blank", error.full_message
+ }
+ end
+
+ test "equality by base attribute, type and options" do
+ person = Person.new
+
+ e1 = ActiveModel::Error.new(person, :name, foo: :bar)
+ e2 = ActiveModel::Error.new(person, :name, foo: :bar)
+ e2.instance_variable_set(:@_humanized_attribute, "Name")
+
+ assert_equal(e1, e2)
+ end
+
+ test "inequality" do
+ person = Person.new
+ error = ActiveModel::Error.new(person, :name, foo: :bar)
+
+ assert error != ActiveModel::Error.new(person, :name, foo: :baz)
+ assert error != ActiveModel::Error.new(person, :name)
+ assert error != ActiveModel::Error.new(person, :title, foo: :bar)
+ assert error != ActiveModel::Error.new(Person.new, :name, foo: :bar)
+ end
+
+ test "comparing against different class would not raise error" do
+ person = Person.new
+ error = ActiveModel::Error.new(person, :name, foo: :bar)
+
+ assert error != person
+ end
+end
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index 947f9bf99b..0837e9db96 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -10,7 +10,7 @@ class ErrorsTest < ActiveModel::TestCase
@errors = ActiveModel::Errors.new(self)
end
- attr_accessor :name, :age
+ attr_accessor :name, :age, :gender, :city
attr_reader :errors
def validate!
@@ -31,48 +31,47 @@ class ErrorsTest < ActiveModel::TestCase
end
def test_delete
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "omg"
- errors.delete("foo")
- assert_empty errors[:foo]
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:name, :blank)
+ errors.delete("name")
+ assert_empty errors[:name]
end
def test_include?
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "omg"
+ errors = ActiveModel::Errors.new(Person.new)
+ assert_deprecated { errors[:foo] << "omg" }
assert_includes errors, :foo, "errors should include :foo"
assert_includes errors, "foo", "errors should include 'foo' as :foo"
end
def test_dup
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "bar"
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:name)
errors_dup = errors.dup
- errors_dup[:bar] << "omg"
- assert_not_same errors_dup.messages, errors.messages
+ assert_not_same errors_dup.errors, errors.errors
end
def test_has_key?
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "omg"
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:foo, "omg")
assert_equal true, errors.has_key?(:foo), "errors should have key :foo"
assert_equal true, errors.has_key?("foo"), "errors should have key 'foo' as :foo"
end
def test_has_no_key
- errors = ActiveModel::Errors.new(self)
+ errors = ActiveModel::Errors.new(Person.new)
assert_equal false, errors.has_key?(:name), "errors should not have key :name"
end
def test_key?
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "omg"
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:foo, "omg")
assert_equal true, errors.key?(:foo), "errors should have key :foo"
assert_equal true, errors.key?("foo"), "errors should have key 'foo' as :foo"
end
def test_no_key
- errors = ActiveModel::Errors.new(self)
+ errors = ActiveModel::Errors.new(Person.new)
assert_equal false, errors.key?(:name), "errors should not have key :name"
end
@@ -86,42 +85,58 @@ class ErrorsTest < ActiveModel::TestCase
end
test "error access is indifferent" do
- errors = ActiveModel::Errors.new(self)
- errors[:foo] << "omg"
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:name, "omg")
- assert_equal ["omg"], errors["foo"]
+ assert_equal ["omg"], errors["name"]
end
test "values returns an array of messages" do
+ errors = ActiveModel::Errors.new(Person.new)
+ assert_deprecated { errors.messages[:foo] = "omg" }
+ assert_deprecated { errors.messages[:baz] = "zomg" }
+
+ assert_deprecated do
+ assert_equal ["omg", "zomg"], errors.values
+ end
+ end
+
+ test "[]= overrides values" do
errors = ActiveModel::Errors.new(self)
- errors.messages[:foo] = "omg"
- errors.messages[:baz] = "zomg"
+ assert_deprecated { errors.messages[:foo] = "omg" }
+ assert_deprecated { errors.messages[:foo] = "zomg" }
- assert_equal ["omg", "zomg"], errors.values
+ assert_equal ["zomg"], errors[:foo]
end
test "values returns an empty array after try to get a message only" do
- errors = ActiveModel::Errors.new(self)
+ errors = ActiveModel::Errors.new(Person.new)
errors.messages[:foo]
errors.messages[:baz]
- assert_equal [], errors.values
+ assert_deprecated do
+ assert_equal [], errors.values
+ end
end
test "keys returns the error keys" do
- errors = ActiveModel::Errors.new(self)
- errors.messages[:foo] << "omg"
- errors.messages[:baz] << "zomg"
+ errors = ActiveModel::Errors.new(Person.new)
+ assert_deprecated { errors.messages[:foo] << "omg" }
+ assert_deprecated { errors.messages[:baz] << "zomg" }
- assert_equal [:foo, :baz], errors.keys
+ assert_deprecated do
+ assert_equal [:foo, :baz], errors.keys
+ end
end
test "keys returns an empty array after try to get a message only" do
- errors = ActiveModel::Errors.new(self)
+ errors = ActiveModel::Errors.new(Person.new)
errors.messages[:foo]
errors.messages[:baz]
- assert_equal [], errors.keys
+ assert_deprecated do
+ assert_equal [], errors.keys
+ end
end
test "detecting whether there are errors with empty?, blank?, include?" do
@@ -146,32 +161,108 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal ["cannot be nil"], person.errors[:name]
end
- test "add an error message on a specific attribute" do
+ test "add an error message on a specific attribute (deprecated)" do
person = Person.new
person.errors.add(:name, "cannot be blank")
assert_equal ["cannot be blank"], person.errors[:name]
end
- test "add an error message on a specific attribute with a defined type" do
+ test "add an error message on a specific attribute with a defined type (deprecated)" do
person = Person.new
person.errors.add(:name, :blank, message: "cannot be blank")
assert_equal ["cannot be blank"], person.errors[:name]
end
- test "add an error with a symbol" do
+ test "add an error with a symbol (deprecated)" do
person = Person.new
person.errors.add(:name, :blank)
message = person.errors.generate_message(:name, :blank)
assert_equal [message], person.errors[:name]
end
- test "add an error with a proc" do
+ test "add an error with a proc (deprecated)" do
person = Person.new
message = Proc.new { "cannot be blank" }
person.errors.add(:name, message)
assert_equal ["cannot be blank"], person.errors[:name]
end
+ test "add creates an error object and returns it" do
+ person = Person.new
+ error = person.errors.add(:name, :blank)
+
+ assert_equal :name, error.attribute
+ assert_equal :blank, error.type
+ assert_equal error, person.errors.objects.first
+ end
+
+ test "add, with type as symbol" do
+ person = Person.new
+ person.errors.add(:name, :blank)
+
+ assert_equal :blank, person.errors.objects.first.type
+ assert_equal ["can't be blank"], person.errors[:name]
+ end
+
+ test "add, with type as String" do
+ msg = "custom msg"
+
+ person = Person.new
+ person.errors.add(:name, msg)
+
+ assert_equal [msg], person.errors[:name]
+ end
+
+ test "add, with type as nil" do
+ person = Person.new
+ person.errors.add(:name)
+
+ assert_equal :invalid, person.errors.objects.first.type
+ assert_equal ["is invalid"], person.errors[:name]
+ end
+
+ test "add, with type as Proc, which evaluates to String" do
+ msg = "custom msg"
+ type = Proc.new { msg }
+
+ person = Person.new
+ person.errors.add(:name, type)
+
+ assert_equal [msg], person.errors[:name]
+ end
+
+ test "add, type being Proc, which evaluates to Symbol" do
+ type = Proc.new { :blank }
+
+ person = Person.new
+ person.errors.add(:name, type)
+
+ assert_equal :blank, person.errors.objects.first.type
+ assert_equal ["can't be blank"], person.errors[:name]
+ end
+
+ test "initialize options[:message] as Proc, which evaluates to String" do
+ msg = "custom msg"
+ type = Proc.new { msg }
+
+ person = Person.new
+ person.errors.add(:name, :blank, message: type)
+
+ assert_equal :blank, person.errors.objects.first.type
+ assert_equal [msg], person.errors[:name]
+ end
+
+ test "add, with options[:message] as Proc, which evaluates to String, where type is nil" do
+ msg = "custom msg"
+ type = Proc.new { msg }
+
+ person = Person.new
+ person.errors.add(:name, message: type)
+
+ assert_equal :invalid, person.errors.objects.first.type
+ assert_equal [msg], person.errors[:name]
+ end
+
test "added? detects indifferent if a specific error was added to the object" do
person = Person.new
person.errors.add(:name, "cannot be blank")
@@ -437,6 +528,32 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal({ name: [{ error: :invalid }] }, person.errors.details)
end
+ test "details retains original type as error" do
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:name, "cannot be nil")
+ errors.add("foo", "bar")
+ errors.add(:baz, nil)
+ errors.add(:age, :invalid, count: 3, message: "%{count} is too low")
+
+ assert_equal(
+ {
+ name: [{ error: "cannot be nil" }],
+ foo: [{ error: "bar" }],
+ baz: [{ error: nil }],
+ age: [{ error: :invalid, count: 3 }]
+ },
+ errors.details
+ )
+ end
+
+ test "group_by_attribute" do
+ person = Person.new
+ error = person.errors.add(:name, :invalid, message: "is bad")
+ hash = person.errors.group_by_attribute
+
+ assert_equal({ name: [error] }, hash)
+ end
+
test "dup duplicates details" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)
@@ -449,7 +566,7 @@ class ErrorsTest < ActiveModel::TestCase
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)
errors.delete(:name)
- assert_empty errors.details[:name]
+ assert_not errors.added?(:name)
end
test "delete returns the deleted messages" do
@@ -467,7 +584,7 @@ class ErrorsTest < ActiveModel::TestCase
assert_empty person.errors.details
end
- test "copy errors" do
+ test "copy errors (deprecated)" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)
person = Person.new
@@ -477,7 +594,25 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal [:name], person.errors.details.keys
end
- test "merge errors" do
+ test "details returns empty array when accessed with non-existent attribute" do
+ errors = ActiveModel::Errors.new(Person.new)
+
+ assert_equal [], errors.details[:foo]
+ end
+
+ test "copy errors" do
+ errors = ActiveModel::Errors.new(Person.new)
+ errors.add(:name, :invalid)
+ person = Person.new
+ person.errors.copy!(errors)
+
+ assert person.errors.added?(:name, :invalid)
+ person.errors.each do |error|
+ assert_same person, error.base
+ end
+ end
+
+ test "merge errors (deprecated)" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, :invalid)
@@ -489,6 +624,18 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details)
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(person.errors.added?(:name, :invalid))
+ assert(person.errors.added?(:name, :blank))
+ end
+
test "slice! removes all errors except the given keys" do
person = Person.new
person.errors.add(:name, "cannot be nil")
@@ -496,9 +643,9 @@ class ErrorsTest < ActiveModel::TestCase
person.errors.add(:gender, "cannot be nil")
person.errors.add(:city, "cannot be nil")
- person.errors.slice!(:age, "gender")
+ assert_deprecated { person.errors.slice!(:age, "gender") }
- assert_equal [:age, :gender], person.errors.keys
+ assert_equal [:age, :gender], assert_deprecated { person.errors.keys }
end
test "slice! returns the deleted errors" do
@@ -508,7 +655,7 @@ class ErrorsTest < ActiveModel::TestCase
person.errors.add(:gender, "cannot be nil")
person.errors.add(:city, "cannot be nil")
- removed_errors = person.errors.slice!(:age, "gender")
+ removed_errors = assert_deprecated { person.errors.slice!(:age, "gender") }
assert_equal({ name: ["cannot be nil"], city: ["cannot be nil"] }, removed_errors)
end
@@ -518,10 +665,23 @@ class ErrorsTest < ActiveModel::TestCase
errors.add(:name, :invalid)
serialized = Marshal.load(Marshal.dump(errors))
+ assert_equal Person, serialized.instance_variable_get(:@base).class
assert_equal errors.messages, serialized.messages
assert_equal errors.details, serialized.details
end
+ test "errors are compatible with marshal dumped from Rails 5.x" do
+ # Derived from
+ # errors = ActiveModel::Errors.new(Person.new)
+ # errors.add(:name, :invalid)
+ dump = "\x04\bU:\x18ActiveModel::Errors[\bo:\x17ErrorsTest::Person\x06:\f@errorsU;\x00[\b@\a{\x00{\x00{\x06:\tname[\x06I\"\x0Fis invalid\x06:\x06ET{\x06;\b[\x06{\x06:\nerror:\finvalid"
+ serialized = Marshal.load(dump)
+
+ assert_equal Person, serialized.instance_variable_get(:@base).class
+ assert_equal({ name: ["is invalid"] }, serialized.messages)
+ assert_equal({ name: [{ error: :invalid }] }, serialized.details)
+ end
+
test "errors are backward compatible with the Rails 4.2 format" do
yaml = <<~CODE
--- !ruby/object:ActiveModel::Errors
@@ -541,4 +701,54 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal({}, errors.messages)
assert_equal({}, errors.details)
end
+
+ test "errors are compatible with YAML dumped from Rails 5.x" do
+ yaml = <<~CODE
+ --- !ruby/object:ActiveModel::Errors
+ base: &1 !ruby/object:ErrorsTest::Person
+ errors: !ruby/object:ActiveModel::Errors
+ base: *1
+ messages: {}
+ details: {}
+ messages:
+ :name:
+ - is invalid
+ details:
+ :name:
+ - :error: :invalid
+ CODE
+
+ errors = YAML.load(yaml)
+ assert_equal({ name: ["is invalid"] }, errors.messages)
+ assert_equal({ name: [{ error: :invalid }] }, errors.details)
+
+ errors.clear
+ assert_equal({}, errors.messages)
+ assert_equal({}, errors.details)
+ end
+
+ test "errors are compatible with YAML dumped from Rails 6.x" do
+ yaml = <<~CODE
+ --- !ruby/object:ActiveModel::Errors
+ base: &1 !ruby/object:ErrorsTest::Person
+ errors: !ruby/object:ActiveModel::Errors
+ base: *1
+ errors: []
+ errors:
+ - !ruby/object:ActiveModel::Error
+ base: *1
+ attribute: :name
+ type: :invalid
+ raw_type: :invalid
+ options: {}
+ CODE
+
+ errors = YAML.load(yaml)
+ assert_equal({ name: ["is invalid"] }, errors.messages)
+ assert_equal({ name: [{ error: :invalid }] }, errors.details)
+
+ errors.clear
+ assert_equal({}, errors.messages)
+ assert_equal({}, errors.details)
+ end
end
diff --git a/activemodel/test/cases/nested_error_test.rb b/activemodel/test/cases/nested_error_test.rb
new file mode 100644
index 0000000000..5bad100da5
--- /dev/null
+++ b/activemodel/test/cases/nested_error_test.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+require "cases/helper"
+require "active_model/nested_error"
+require "models/topic"
+require "models/reply"
+
+class ErrorTest < ActiveModel::TestCase
+ def test_initialize
+ topic = Topic.new
+ inner_error = ActiveModel::Error.new(topic, :title, :not_enough, count: 2)
+ reply = Reply.new
+ error = ActiveModel::NestedError.new(reply, inner_error)
+
+ assert_equal reply, error.base
+ assert_equal inner_error.attribute, error.attribute
+ assert_equal inner_error.type, error.type
+ assert_equal(inner_error.options, error.options)
+ end
+
+ test "initialize with overriding attribute and type" do
+ topic = Topic.new
+ inner_error = ActiveModel::Error.new(topic, :title, :not_enough, count: 2)
+ reply = Reply.new
+ error = ActiveModel::NestedError.new(reply, inner_error, attribute: :parent, type: :foo)
+
+ assert_equal reply, error.base
+ assert_equal :parent, error.attribute
+ assert_equal :foo, error.type
+ assert_equal(inner_error.options, error.options)
+ end
+
+ def test_message
+ topic = Topic.new(author_name: "Bruce")
+ inner_error = ActiveModel::Error.new(topic, :title, :not_enough, message: Proc.new { |model, options|
+ "not good enough for #{model.author_name}"
+ })
+ reply = Reply.new(author_name: "Mark")
+ error = ActiveModel::NestedError.new(reply, inner_error)
+
+ assert_equal "not good enough for Bruce", error.message
+ end
+
+ def test_full_message
+ topic = Topic.new(author_name: "Bruce")
+ inner_error = ActiveModel::Error.new(topic, :title, :not_enough, message: Proc.new { |model, options|
+ "not good enough for #{model.author_name}"
+ })
+ reply = Reply.new(author_name: "Mark")
+ error = ActiveModel::NestedError.new(reply, inner_error)
+
+ assert_equal "Title not good enough for Bruce", error.full_message
+ end
+end
diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb
index 35bb918f26..b7ee50832c 100644
--- a/activemodel/test/cases/validations/i18n_validation_test.rb
+++ b/activemodel/test/cases/validations/i18n_validation_test.rb
@@ -169,8 +169,8 @@ class I18nValidationTest < ActiveModel::TestCase
# [ case, validation_options, generate_message_options]
[ "given no options", {}, {}],
[ "given custom message", { message: "custom" }, { message: "custom" }],
- [ "given if condition", { if: lambda { true } }, {}],
- [ "given unless condition", { unless: lambda { false } }, {}],
+ [ "given if condition", { if: lambda { true } }, {}],
+ [ "given unless condition", { unless: lambda { false } }, {}],
[ "given option that is not reserved", { format: "jpg" }, { format: "jpg" }]
]
@@ -181,6 +181,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title_confirmation, :confirmation, generate_message_options.merge(attribute: "Title")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -191,6 +192,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :accepted, generate_message_options]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -201,6 +203,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :blank, generate_message_options]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -211,6 +214,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :too_short, generate_message_options.merge(count: 3)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -222,6 +226,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :too_long, generate_message_options.merge(count: 5)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -232,6 +237,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :wrong_length, generate_message_options.merge(count: 5)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -243,6 +249,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :invalid, generate_message_options.merge(value: "72x")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -254,6 +261,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :inclusion, generate_message_options.merge(value: "z")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -265,6 +273,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :inclusion, generate_message_options.merge(value: "z")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -276,6 +285,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :exclusion, generate_message_options.merge(value: "a")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -287,6 +297,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :exclusion, generate_message_options.merge(value: "a")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -298,6 +309,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :not_a_number, generate_message_options.merge(value: "a")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -309,6 +321,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :not_an_integer, generate_message_options.merge(value: "0.0")]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -320,6 +333,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :odd, generate_message_options.merge(value: 0)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
@@ -331,6 +345,7 @@ class I18nValidationTest < ActiveModel::TestCase
call = [:title, :less_than, generate_message_options.merge(value: 1, count: 0)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
+ @person.errors.messages
end
end
end
diff --git a/activemodel/test/cases/validations/validations_context_test.rb b/activemodel/test/cases/validations/validations_context_test.rb
index 024eb1882f..3d2dea9828 100644
--- a/activemodel/test/cases/validations/validations_context_test.rb
+++ b/activemodel/test/cases/validations/validations_context_test.rb
@@ -14,13 +14,13 @@ class ValidationsContextTest < ActiveModel::TestCase
class ValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
- record.errors[:base] << ERROR_MESSAGE
+ record.errors.add(:base, ERROR_MESSAGE)
end
end
class AnotherValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
- record.errors[:base] << ANOTHER_ERROR_MESSAGE
+ record.errors.add(:base, ANOTHER_ERROR_MESSAGE)
end
end
diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb
index 8239792c79..e6ae6603f2 100644
--- a/activemodel/test/cases/validations/with_validation_test.rb
+++ b/activemodel/test/cases/validations/with_validation_test.rb
@@ -14,13 +14,13 @@ class ValidatesWithTest < ActiveModel::TestCase
class ValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
- record.errors[:base] << ERROR_MESSAGE
+ record.errors.add(:base, message: ERROR_MESSAGE)
end
end
class OtherValidatorThatAddsErrors < ActiveModel::Validator
def validate(record)
- record.errors[:base] << OTHER_ERROR_MESSAGE
+ record.errors.add(:base, message: OTHER_ERROR_MESSAGE)
end
end
@@ -32,14 +32,14 @@ class ValidatesWithTest < ActiveModel::TestCase
class ValidatorThatValidatesOptions < ActiveModel::Validator
def validate(record)
if options[:field] == :first_name
- record.errors[:base] << ERROR_MESSAGE
+ record.errors.add(:base, message: ERROR_MESSAGE)
end
end
end
class ValidatorPerEachAttribute < ActiveModel::EachValidator
def validate_each(record, attribute, value)
- record.errors[attribute] << "Value is #{value}"
+ record.errors.add(attribute, message: "Value is #{value}")
end
end
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index 7776233db5..0b9e1b7005 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -53,7 +53,7 @@ class ValidationsTest < ActiveModel::TestCase
r = Reply.new
r.valid?
- errors = r.errors.collect { |attr, messages| [attr.to_s, messages] }
+ errors = assert_deprecated { r.errors.collect { |attr, messages| [attr.to_s, messages] } }
assert_includes errors, ["title", "is Empty"]
assert_includes errors, ["content", "is Empty"]
@@ -74,7 +74,7 @@ class ValidationsTest < ActiveModel::TestCase
def test_errors_on_nested_attributes_expands_name
t = Topic.new
- t.errors["replies.name"] << "can't be blank"
+ assert_deprecated { t.errors["replies.name"] << "can't be blank" }
assert_equal ["Replies name can't be blank"], t.errors.full_messages
end
@@ -216,7 +216,7 @@ class ValidationsTest < ActiveModel::TestCase
t = Topic.new
assert_predicate t, :invalid?
- xml = t.errors.to_xml
+ xml = assert_deprecated { t.errors.to_xml }
assert_match %r{<errors>}, xml
assert_match %r{<error>Title can't be blank</error>}, xml
assert_match %r{<error>Content can't be blank</error>}, xml
@@ -241,14 +241,14 @@ class ValidationsTest < ActiveModel::TestCase
t = Topic.new title: ""
assert_predicate t, :invalid?
- assert_equal :title, key = t.errors.keys[0]
+ assert_equal :title, key = assert_deprecated { t.errors.keys[0] }
assert_equal "can't be blank", t.errors[key][0]
assert_equal "is too short (minimum is 2 characters)", t.errors[key][1]
- assert_equal :author_name, key = t.errors.keys[1]
+ assert_equal :author_name, key = assert_deprecated { t.errors.keys[1] }
assert_equal "can't be blank", t.errors[key][0]
- assert_equal :author_email_address, key = t.errors.keys[2]
+ assert_equal :author_email_address, key = assert_deprecated { t.errors.keys[2] }
assert_equal "will never be valid", t.errors[key][0]
- assert_equal :content, key = t.errors.keys[3]
+ assert_equal :content, key = assert_deprecated { t.errors.keys[3] }
assert_equal "is too short (minimum is 2 characters)", t.errors[key][0]
end
diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb
index 44e78cbc29..fbb28d2a0f 100644
--- a/activemodel/test/models/person_with_validator.rb
+++ b/activemodel/test/models/person_with_validator.rb
@@ -5,7 +5,7 @@ class PersonWithValidator
class PresenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
- record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank?
+ record.errors.add(attribute, message: "Local validator#{options[:custom]}") if value.blank?
end
end
diff --git a/activemodel/test/models/reply.rb b/activemodel/test/models/reply.rb
index 6bb18f95fe..f340b6fb14 100644
--- a/activemodel/test/models/reply.rb
+++ b/activemodel/test/models/reply.rb
@@ -11,24 +11,24 @@ class Reply < Topic
validate :check_wrong_update, on: :update
def check_empty_title
- errors[:title] << "is Empty" unless title && title.size > 0
+ errors.add(:title, "is Empty") unless title && title.size > 0
end
def errors_on_empty_content
- errors[:content] << "is Empty" unless content && content.size > 0
+ errors.add(:content, "is Empty") unless content && content.size > 0
end
def check_content_mismatch
if title && content && content == "Mismatch"
- errors[:title] << "is Content Mismatch"
+ errors.add(:title, "is Content Mismatch")
end
end
def title_is_wrong_create
- errors[:title] << "is Wrong Create" if title && title == "Wrong Create"
+ errors.add(:title, "is Wrong Create") if title && title == "Wrong Create"
end
def check_wrong_update
- errors[:title] << "is Wrong Update" if title && title == "Wrong Update"
+ errors.add(:title, "is Wrong Update") if title && title == "Wrong Update"
end
end
diff --git a/activemodel/test/validators/email_validator.rb b/activemodel/test/validators/email_validator.rb
index 0c634d8659..774a10b2ba 100644
--- a/activemodel/test/validators/email_validator.rb
+++ b/activemodel/test/validators/email_validator.rb
@@ -2,7 +2,8 @@
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
- record.errors[attribute] << (options[:message] || "is not an email") unless
- /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.match?(value)
+ unless /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.match?(value)
+ record.errors.add(attribute, message: options[:message] || "is not an email")
+ end
end
end