2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/topic"
|
|
|
|
require "models/person"
|
|
|
|
require "models/custom_reader"
|
2012-07-25 11:07:05 -04:00
|
|
|
|
|
|
|
class AbsenceValidationTest < ActiveModel::TestCase
|
|
|
|
teardown do
|
2014-01-27 05:22:26 -05:00
|
|
|
Topic.clear_validators!
|
|
|
|
Person.clear_validators!
|
|
|
|
CustomReader.clear_validators!
|
2012-07-25 11:07:05 -04:00
|
|
|
end
|
|
|
|
|
2014-06-15 22:52:11 -04:00
|
|
|
def test_validates_absence_of
|
2012-07-25 11:07:05 -04:00
|
|
|
Topic.validates_absence_of(:title, :content)
|
|
|
|
t = Topic.new
|
|
|
|
t.title = "foo"
|
|
|
|
t.content = "bar"
|
|
|
|
assert t.invalid?
|
|
|
|
assert_equal ["must be blank"], t.errors[:title]
|
|
|
|
assert_equal ["must be blank"], t.errors[:content]
|
|
|
|
t.title = ""
|
2016-10-28 23:05:58 -04:00
|
|
|
t.content = "something"
|
2012-07-25 11:07:05 -04:00
|
|
|
assert t.invalid?
|
|
|
|
assert_equal ["must be blank"], t.errors[:content]
|
2014-06-15 22:52:11 -04:00
|
|
|
assert_equal [], t.errors[:title]
|
2012-07-25 11:07:05 -04:00
|
|
|
t.content = ""
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
2014-06-15 22:52:11 -04:00
|
|
|
def test_validates_absence_of_with_array_arguments
|
2012-07-25 11:07:05 -04:00
|
|
|
Topic.validates_absence_of %w(title content)
|
|
|
|
t = Topic.new
|
|
|
|
t.title = "foo"
|
|
|
|
t.content = "bar"
|
|
|
|
assert t.invalid?
|
|
|
|
assert_equal ["must be blank"], t.errors[:title]
|
|
|
|
assert_equal ["must be blank"], t.errors[:content]
|
|
|
|
end
|
|
|
|
|
2014-06-15 22:52:11 -04:00
|
|
|
def test_validates_absence_of_with_custom_error_using_quotes
|
2012-07-25 11:07:05 -04:00
|
|
|
Person.validates_absence_of :karma, message: "This string contains 'single' and \"double\" quotes"
|
|
|
|
p = Person.new
|
|
|
|
p.karma = "good"
|
|
|
|
assert p.invalid?
|
|
|
|
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_absence_of_for_ruby_class
|
|
|
|
Person.validates_absence_of :karma
|
|
|
|
p = Person.new
|
|
|
|
p.karma = "good"
|
|
|
|
assert p.invalid?
|
|
|
|
assert_equal ["must be blank"], p.errors[:karma]
|
|
|
|
p.karma = nil
|
|
|
|
assert p.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_absence_of_for_ruby_class_with_custom_reader
|
|
|
|
CustomReader.validates_absence_of :karma
|
|
|
|
p = CustomReader.new
|
|
|
|
p[:karma] = "excellent"
|
|
|
|
assert p.invalid?
|
|
|
|
assert_equal ["must be blank"], p.errors[:karma]
|
|
|
|
p[:karma] = ""
|
|
|
|
assert p.valid?
|
|
|
|
end
|
|
|
|
end
|