aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/nested_error_test.rb
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/cases/nested_error_test.rb
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/cases/nested_error_test.rb')
-rw-r--r--activemodel/test/cases/nested_error_test.rb54
1 files changed, 54 insertions, 0 deletions
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