class ValidatesTest

Public Instance Methods

reset_callbacks() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 13
def reset_callbacks
  Person.clear_validators!
  Topic.clear_validators!
  PersonWithValidator.clear_validators!
end
test_defining_extra_default_keys_for_validates() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 151
def test_defining_extra_default_keys_for_validates
  Topic.validates :title, confirmation: true, message: "Y U NO CONFIRM"
  topic = Topic.new
  topic.title = "What's happening"
  topic.title_confirmation = "Not this"
  assert !topic.valid?
  assert_equal ["Y U NO CONFIRM"], topic.errors[:title_confirmation]
end
test_validates_with_allow_nil_shared_conditions() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 84
def test_validates_with_allow_nil_shared_conditions
  Person.validates :karma, length: { minimum: 20 }, email: true, allow_nil: true
  person = Person.new
  assert person.valid?
end
test_validates_with_array() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 99
def test_validates_with_array
  Person.validates :gender, inclusion: %w(m f)
  person = Person.new
  assert person.invalid?
  assert_equal ["is not included in the list"], person.errors[:gender]
  person.gender = "m"
  assert person.valid?
end
test_validates_with_attribute_specified_as_string() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 32
def test_validates_with_attribute_specified_as_string
  Person.validates "title", numericality: true
  person = Person.new
  person.valid?
  assert_equal ["is not a number"], person.errors[:title]

  person = Person.new
  person.title = 123
  assert person.valid?
end
test_validates_with_built_in_validation() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 25
def test_validates_with_built_in_validation
  Person.validates :title, numericality: true
  person = Person.new
  person.valid?
  assert_equal ["is not a number"], person.errors[:title]
end
test_validates_with_built_in_validation_and_options() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 43
def test_validates_with_built_in_validation_and_options
  Person.validates :salary, numericality: { message: "my custom message" }
  person = Person.new
  person.valid?
  assert_equal ["my custom message"], person.errors[:salary]
end
test_validates_with_if_as_local_conditions() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 64
def test_validates_with_if_as_local_conditions
  Person.validates :karma, presence: true, email: { unless: :condition_is_true }
  person = Person.new
  person.valid?
  assert_equal ["can't be blank"], person.errors[:karma]
end
test_validates_with_if_as_shared_conditions() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 71
def test_validates_with_if_as_shared_conditions
  Person.validates :karma, presence: true, email: true, if: :condition_is_true
  person = Person.new
  person.valid?
  assert_equal ["can't be blank", "is not an email"], person.errors[:karma].sort
end
test_validates_with_included_validator() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 128
def test_validates_with_included_validator
  PersonWithValidator.validates :title, presence: true
  person = PersonWithValidator.new
  person.valid?
  assert_equal ["Local validator"], person.errors[:title]
end
test_validates_with_included_validator_and_options() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 135
def test_validates_with_included_validator_and_options
  PersonWithValidator.validates :title, presence: { custom: " please" }
  person = PersonWithValidator.new
  person.valid?
  assert_equal ["Local validator please"], person.errors[:title]
end
test_validates_with_included_validator_and_wildcard_shortcut() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 142
def test_validates_with_included_validator_and_wildcard_shortcut
  # Shortcut for PersonWithValidator.validates :title, like: { with: "Mr." }
  PersonWithValidator.validates :title, like: "Mr."
  person = PersonWithValidator.new
  person.title = "Ms. Pacman"
  person.valid?
  assert_equal ["does not appear to be like Mr."], person.errors[:title]
end
test_validates_with_messages_empty() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 19
def test_validates_with_messages_empty
  Person.validates :title, presence: { message: "" }
  person = Person.new
  assert !person.valid?, "person should not be valid."
end
test_validates_with_namespaced_validator_class() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 57
def test_validates_with_namespaced_validator_class
  Person.validates :karma, 'namespace/email': true
  person = Person.new
  person.valid?
  assert_equal ["is not an email"], person.errors[:karma]
end
test_validates_with_range() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 108
def test_validates_with_range
  Person.validates :karma, length: 6..20
  person = Person.new
  assert person.invalid?
  assert_equal ["is too short (minimum is 6 characters)"], person.errors[:karma]
  person.karma = "something"
  assert person.valid?
end
test_validates_with_regexp() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 90
def test_validates_with_regexp
  Person.validates :karma, format: /positive|negative/
  person = Person.new
  assert person.invalid?
  assert_equal ["is invalid"], person.errors[:karma]
  person.karma = "positive"
  assert person.valid?
end
test_validates_with_unknown_validator() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 124
def test_validates_with_unknown_validator
  assert_raise(ArgumentError) { Person.validates :karma, unknown: true }
end
test_validates_with_unless_shared_conditions() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 78
def test_validates_with_unless_shared_conditions
  Person.validates :karma, presence: true, email: true, unless: :condition_is_true
  person = Person.new
  assert person.valid?
end
test_validates_with_validator_class() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 50
def test_validates_with_validator_class
  Person.validates :karma, email: true
  person = Person.new
  person.valid?
  assert_equal ["is not an email"], person.errors[:karma]
end
test_validates_with_validator_class_and_options() click to toggle source
# File activemodel/test/cases/validations/validates_test.rb, line 117
def test_validates_with_validator_class_and_options
  Person.validates :karma, email: { message: "my custom message" }
  person = Person.new
  person.valid?
  assert_equal ["my custom message"], person.errors[:karma]
end