2017-07-16 17:11:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 16:38:23 +00:00
|
|
|
require "cases/helper"
|
2009-03-20 15:07:49 +00:00
|
|
|
|
2016-08-06 16:38:23 +00:00
|
|
|
require "models/topic"
|
|
|
|
require "models/person"
|
|
|
|
require "models/custom_reader"
|
2009-03-20 15:07:49 +00:00
|
|
|
|
2019-08-02 04:25:13 +00:00
|
|
|
class PresenceValidationTest < ActiveModel::TestCase
|
2010-02-25 21:07:48 +00:00
|
|
|
teardown do
|
2014-01-27 10:22:26 +00:00
|
|
|
Topic.clear_validators!
|
|
|
|
Person.clear_validators!
|
|
|
|
CustomReader.clear_validators!
|
2010-02-25 21:07:48 +00:00
|
|
|
end
|
|
|
|
|
2009-03-20 15:07:49 +00:00
|
|
|
def test_validate_presences
|
|
|
|
Topic.validates_presence_of(:title, :content)
|
|
|
|
|
2010-05-08 20:27:49 +00:00
|
|
|
t = Topic.new
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate t, :invalid?
|
2009-03-20 15:07:49 +00:00
|
|
|
assert_equal ["can't be blank"], t.errors[:title]
|
|
|
|
assert_equal ["can't be blank"], t.errors[:content]
|
|
|
|
|
|
|
|
t.title = "something"
|
2016-10-29 03:05:58 +00:00
|
|
|
t.content = " "
|
2009-03-20 15:07:49 +00:00
|
|
|
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate t, :invalid?
|
2009-03-20 15:07:49 +00:00
|
|
|
assert_equal ["can't be blank"], t.errors[:content]
|
|
|
|
|
|
|
|
t.content = "like stuff"
|
|
|
|
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate t, :valid?
|
2010-02-25 21:07:48 +00:00
|
|
|
end
|
|
|
|
|
2010-06-26 09:57:43 +00:00
|
|
|
def test_accepts_array_arguments
|
2010-02-25 21:07:48 +00:00
|
|
|
Topic.validates_presence_of %w(title content)
|
|
|
|
t = Topic.new
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate t, :invalid?
|
2010-02-25 21:07:48 +00:00
|
|
|
assert_equal ["can't be blank"], t.errors[:title]
|
|
|
|
assert_equal ["can't be blank"], t.errors[:content]
|
2009-03-20 15:07:49 +00:00
|
|
|
end
|
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
def test_validates_acceptance_of_with_custom_error_using_quotes
|
2012-12-26 21:20:48 +00:00
|
|
|
Person.validates_presence_of :karma, message: "This string contains 'single' and \"double\" quotes"
|
2009-12-23 12:30:58 +00:00
|
|
|
p = Person.new
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate p, :invalid?
|
2009-12-23 12:30:58 +00:00
|
|
|
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
|
|
|
|
end
|
2009-03-20 22:21:27 +00:00
|
|
|
|
|
|
|
def test_validates_presence_of_for_ruby_class
|
2009-12-23 12:30:58 +00:00
|
|
|
Person.validates_presence_of :karma
|
2009-03-20 22:21:27 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
p = Person.new
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate p, :invalid?
|
2009-03-20 22:21:27 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
assert_equal ["can't be blank"], p.errors[:karma]
|
2009-03-20 22:21:27 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
p.karma = "Cold"
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate p, :valid?
|
2009-03-20 22:21:27 +00:00
|
|
|
end
|
2010-02-25 21:07:48 +00:00
|
|
|
|
2009-08-05 16:44:44 +00:00
|
|
|
def test_validates_presence_of_for_ruby_class_with_custom_reader
|
2009-12-23 12:30:58 +00:00
|
|
|
CustomReader.validates_presence_of :karma
|
2009-08-05 16:44:44 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
p = CustomReader.new
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate p, :invalid?
|
2009-08-05 16:44:44 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
assert_equal ["can't be blank"], p.errors[:karma]
|
2009-08-05 16:44:44 +00:00
|
|
|
|
2009-12-23 12:30:58 +00:00
|
|
|
p[:karma] = "Cold"
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate p, :valid?
|
2009-08-05 16:44:44 +00:00
|
|
|
end
|
2012-12-26 18:31:05 +00:00
|
|
|
|
2012-12-26 22:08:08 +00:00
|
|
|
def test_validates_presence_of_with_allow_nil_option
|
2012-12-26 21:20:48 +00:00
|
|
|
Topic.validates_presence_of(:title, allow_nil: true)
|
2012-12-26 18:31:05 +00:00
|
|
|
|
2012-12-26 21:20:48 +00:00
|
|
|
t = Topic.new(title: "something")
|
2012-12-26 18:31:05 +00:00
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
|
|
|
|
t.title = ""
|
2018-01-25 23:14:09 +00:00
|
|
|
assert_predicate t, :invalid?
|
2012-12-26 18:31:05 +00:00
|
|
|
assert_equal ["can't be blank"], t.errors[:title]
|
|
|
|
|
|
|
|
t.title = " "
|
|
|
|
assert t.invalid?, t.errors.full_messages
|
|
|
|
assert_equal ["can't be blank"], t.errors[:title]
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
end
|
|
|
|
|
2012-12-26 22:08:08 +00:00
|
|
|
def test_validates_presence_of_with_allow_blank_option
|
2012-12-26 21:20:48 +00:00
|
|
|
Topic.validates_presence_of(:title, allow_blank: true)
|
2012-12-26 18:31:05 +00:00
|
|
|
|
2012-12-26 21:20:48 +00:00
|
|
|
t = Topic.new(title: "something")
|
2012-12-26 18:31:05 +00:00
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
|
|
|
|
t.title = ""
|
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
|
|
|
|
t.title = " "
|
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?, t.errors.full_messages
|
|
|
|
end
|
2009-03-20 15:07:49 +00:00
|
|
|
end
|