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