2010-08-03 04:32:48 -04:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
class ErrorsTest < ActiveModel::TestCase
|
|
|
|
class Person
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
def initialize
|
|
|
|
@errors = ActiveModel::Errors.new(self)
|
|
|
|
end
|
|
|
|
|
2012-11-05 10:29:31 -05:00
|
|
|
attr_accessor :name, :age
|
2010-08-03 04:32:48 -04:00
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
def validate!
|
2014-01-03 17:02:31 -05:00
|
|
|
errors.add(:name, "cannot be nil") if name == nil
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_attribute_for_validation(attr)
|
|
|
|
send(attr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.human_attribute_name(attr, options = {})
|
|
|
|
attr
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_ancestors
|
|
|
|
[self]
|
|
|
|
end
|
2011-01-05 06:56:25 -05:00
|
|
|
end
|
2010-08-03 04:32:48 -04:00
|
|
|
|
2012-01-16 11:22:44 -05:00
|
|
|
def test_delete
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = 'omg'
|
|
|
|
errors.delete(:foo)
|
|
|
|
assert_empty errors[:foo]
|
|
|
|
end
|
|
|
|
|
2011-02-09 12:19:15 -05:00
|
|
|
def test_include?
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = 'omg'
|
|
|
|
assert errors.include?(:foo), 'errors should include :foo'
|
|
|
|
end
|
|
|
|
|
2012-01-17 02:07:15 -05:00
|
|
|
def test_dup
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = 'bar'
|
|
|
|
errors_dup = errors.dup
|
|
|
|
errors_dup[:bar] = 'omg'
|
|
|
|
assert_not_same errors_dup.messages, errors.messages
|
|
|
|
end
|
|
|
|
|
2011-09-06 12:35:12 -04:00
|
|
|
def test_has_key?
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = 'omg'
|
2014-01-22 15:25:58 -05:00
|
|
|
assert_equal true, errors.has_key?(:foo), 'errors should have key :foo'
|
2011-09-06 12:35:12 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 09:22:35 -05:00
|
|
|
def test_has_no_key
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
assert_equal false, errors.has_key?(:name), 'errors should not have key :name'
|
|
|
|
end
|
|
|
|
|
2014-10-14 04:53:26 -04:00
|
|
|
def test_key?
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = 'omg'
|
|
|
|
assert_equal true, errors.key?(:foo), 'errors should have key :foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_key
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
assert_equal false, errors.key?(:name), 'errors should not have key :name'
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "clear errors" do
|
2013-01-10 14:49:47 -05:00
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.count
|
|
|
|
person.errors.clear
|
|
|
|
assert person.errors.empty?
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "get returns the errors for the provided key" do
|
2013-01-10 14:49:47 -05:00
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = "omg"
|
|
|
|
|
|
|
|
assert_equal ["omg"], errors.get(:foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "sets the error with the provided key" do
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors.set(:foo, "omg")
|
|
|
|
|
|
|
|
assert_equal({ foo: "omg" }, errors.messages)
|
|
|
|
end
|
|
|
|
|
2013-08-19 03:52:31 -04:00
|
|
|
test "error access is indifferent" do
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors[:foo] = "omg"
|
|
|
|
|
|
|
|
assert_equal ["omg"], errors["foo"]
|
|
|
|
end
|
|
|
|
|
2013-01-10 14:49:47 -05:00
|
|
|
test "values returns an array of messages" do
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors.set(:foo, "omg")
|
|
|
|
errors.set(:baz, "zomg")
|
|
|
|
|
|
|
|
assert_equal ["omg", "zomg"], errors.values
|
|
|
|
end
|
|
|
|
|
|
|
|
test "keys returns the error keys" do
|
|
|
|
errors = ActiveModel::Errors.new(self)
|
|
|
|
errors.set(:foo, "omg")
|
|
|
|
errors.set(:baz, "zomg")
|
|
|
|
|
|
|
|
assert_equal [:foo, :baz], errors.keys
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "detecting whether there are errors with empty?, blank?, include?" do
|
2011-01-05 06:56:25 -05:00
|
|
|
person = Person.new
|
|
|
|
person.errors[:foo]
|
|
|
|
assert person.errors.empty?
|
|
|
|
assert person.errors.blank?
|
2011-07-27 12:07:47 -04:00
|
|
|
assert !person.errors.include?(:foo)
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "adding errors using conditionals with Person#validate!" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
|
|
|
person.validate!
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal ["name cannot be nil"], person.errors.full_messages
|
|
|
|
assert_equal ["cannot be nil"], person.errors[:name]
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "assign error" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors[:name] = 'should not be nil'
|
|
|
|
assert_equal ["should not be nil"], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "add an error message on a specific attribute" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_equal ["cannot be blank"], person.errors[:name]
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "add an error with a symbol" do
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
message = person.errors.generate_message(:name, :blank)
|
|
|
|
assert_equal [message], person.errors[:name]
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "add an error with a proc" do
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
message = Proc.new { "cannot be blank" }
|
2011-10-11 15:44:22 -04:00
|
|
|
person.errors.add(:name, message)
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal ["cannot be blank"], person.errors[:name]
|
2011-10-11 15:44:22 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? detects if a specific error was added to the object" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert person.errors.added?(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? handles symbol message" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :blank)
|
|
|
|
assert person.errors.added?(:name, :blank)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? handles proc messages" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
message = Proc.new { "cannot be blank" }
|
2011-10-19 12:47:28 -04:00
|
|
|
person.errors.add(:name, message)
|
|
|
|
assert person.errors.added?(:name, message)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? defaults message to :invalid" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2012-12-01 19:27:55 -05:00
|
|
|
person.errors.add(:name)
|
2011-10-19 12:47:28 -04:00
|
|
|
assert person.errors.added?(:name)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? matches the given message when several errors are present for the same attribute" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
person.errors.add(:name, "is invalid")
|
2014-01-03 17:02:31 -05:00
|
|
|
assert person.errors.added?(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? returns false when no errors are present" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
|
|
|
assert !person.errors.added?(:name)
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do
|
2011-10-19 12:47:28 -04:00
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, "is invalid")
|
2014-01-03 17:02:31 -05:00
|
|
|
assert !person.errors.added?(:name, "cannot be blank")
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "size calculates the number of error messages" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2010-08-03 04:32:48 -04:00
|
|
|
assert_equal 1, person.errors.size
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "to_a returns the list of errors with complete messages containing the attribute names" do
|
2010-08-03 04:32:48 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.to_a
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "to_hash returns the error messages hash" do
|
2010-11-28 08:36:40 -05:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
assert_equal({ name: ["cannot be blank"] }, person.errors.to_hash)
|
2010-11-28 08:36:40 -05:00
|
|
|
end
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages creates a list of error messages with the attribute name included" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages
|
2011-09-09 04:28:25 -04:00
|
|
|
end
|
2013-03-30 14:04:08 -04:00
|
|
|
|
|
|
|
test "full_messages_for contains all the error messages for the given attribute" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:name, "cannot be nil")
|
|
|
|
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages_for(:name)
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages_for does not contain error messages from other attributes" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
|
|
|
person.errors.add(:email, "cannot be blank")
|
|
|
|
assert_equal ["name cannot be blank"], person.errors.full_messages_for(:name)
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_messages_for returns an empty list in case there are no errors for the given attribute" do
|
2013-03-24 03:34:27 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
person.errors.add(:name, "cannot be blank")
|
2013-03-24 03:34:27 -04:00
|
|
|
assert_equal [], person.errors.full_messages_for(:email)
|
|
|
|
end
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_message returns the given message when attribute is :base" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
|
|
|
assert_equal "press the button", person.errors.full_message(:base, "press the button")
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "full_message returns the given message with the attribute name included" do
|
2011-09-09 04:28:25 -04:00
|
|
|
person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal "name cannot be blank", person.errors.full_message(:name, "cannot be blank")
|
|
|
|
assert_equal "name_test cannot be blank", person.errors.full_message(:name_test, "cannot be blank")
|
2011-09-09 04:28:25 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "as_json creates a json formatted representation of the errors hash" do
|
2011-09-09 04:34:13 -04:00
|
|
|
person = Person.new
|
2013-03-30 14:04:08 -04:00
|
|
|
person.validate!
|
|
|
|
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal({ name: ["cannot be nil"] }, person.errors.as_json)
|
2011-09-09 04:34:13 -04:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "as_json with :full_messages option creates a json formatted representation of the errors containing complete messages" do
|
2012-02-17 05:12:10 -05:00
|
|
|
person = Person.new
|
2013-03-30 14:04:08 -04:00
|
|
|
person.validate!
|
|
|
|
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal({ name: ["name cannot be nil"] }, person.errors.as_json(full_messages: true))
|
2012-02-17 05:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:04:08 -04:00
|
|
|
test "generate_message works without i18n_scope" do
|
2011-10-11 15:44:22 -04:00
|
|
|
person = Person.new
|
|
|
|
assert !Person.respond_to?(:i18n_scope)
|
|
|
|
assert_nothing_raised {
|
|
|
|
person.errors.generate_message(:name, :blank)
|
|
|
|
}
|
|
|
|
end
|
2012-11-05 10:29:31 -05:00
|
|
|
|
|
|
|
test "add_on_empty generates message" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.expects(:generate_message).with(:name, :empty, {})
|
|
|
|
person.errors.add_on_empty :name
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add_on_empty generates message for multiple attributes" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.expects(:generate_message).with(:name, :empty, {})
|
|
|
|
person.errors.expects(:generate_message).with(:age, :empty, {})
|
|
|
|
person.errors.add_on_empty [:name, :age]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add_on_empty generates message with custom default message" do
|
|
|
|
person = Person.new
|
2013-05-01 20:10:06 -04:00
|
|
|
person.errors.expects(:generate_message).with(:name, :empty, { message: 'custom' })
|
|
|
|
person.errors.add_on_empty :name, message: 'custom'
|
2012-11-05 10:29:31 -05:00
|
|
|
end
|
|
|
|
|
2013-02-19 12:24:31 -05:00
|
|
|
test "add_on_empty generates message with empty string value" do
|
|
|
|
person = Person.new
|
|
|
|
person.name = ''
|
|
|
|
person.errors.expects(:generate_message).with(:name, :empty, {})
|
|
|
|
person.errors.add_on_empty :name
|
|
|
|
end
|
|
|
|
|
2012-11-05 10:29:31 -05:00
|
|
|
test "add_on_blank generates message" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.expects(:generate_message).with(:name, :blank, {})
|
|
|
|
person.errors.add_on_blank :name
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add_on_blank generates message for multiple attributes" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.expects(:generate_message).with(:name, :blank, {})
|
|
|
|
person.errors.expects(:generate_message).with(:age, :blank, {})
|
|
|
|
person.errors.add_on_blank [:name, :age]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add_on_blank generates message with custom default message" do
|
|
|
|
person = Person.new
|
2013-05-01 20:10:06 -04:00
|
|
|
person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' })
|
|
|
|
person.errors.add_on_blank :name, message: 'custom'
|
2012-11-05 10:29:31 -05:00
|
|
|
end
|
2015-01-04 03:18:03 -05:00
|
|
|
|
|
|
|
test "details returns added error detail" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid)
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "details returns added error detail with custom option" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :greater_than, count: 5)
|
|
|
|
assert_equal({ name: [{ error: :greater_than, count: 5 }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "details do not include message option" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid, message: "is bad")
|
|
|
|
assert_equal({ name: [{ error: :invalid }] }, person.errors.details)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "dup duplicates details" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
errors_dup = errors.dup
|
|
|
|
errors_dup.add(:name, :taken)
|
|
|
|
assert_not_same errors_dup.details, errors.details
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete removes details on given attribute" do
|
|
|
|
errors = ActiveModel::Errors.new(Person.new)
|
|
|
|
errors.add(:name, :invalid)
|
|
|
|
errors.delete(:name)
|
|
|
|
assert_empty errors.details[:name]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "clear removes details" do
|
|
|
|
person = Person.new
|
|
|
|
person.errors.add(:name, :invalid)
|
|
|
|
|
|
|
|
assert_equal 1, person.errors.details.count
|
|
|
|
person.errors.clear
|
|
|
|
assert person.errors.details.empty?
|
|
|
|
end
|
2010-08-03 04:32:48 -04:00
|
|
|
end
|