aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/validates_test.rb
blob: 666c48c8a00887305b73b003022c03f687759110 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# encoding: utf-8
require 'cases/helper'
require 'models/person'
require 'models/person_with_validator'
require 'validators/email_validator'

class ValidatesTest < ActiveModel::TestCase
  setup :reset_callbacks
  teardown :reset_callbacks

  def reset_callbacks
    Person.reset_callbacks(:validate)
    PersonWithValidator.reset_callbacks(:validate)
  end

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  def test_validates_with_unknown_validator
    assert_raise(ArgumentError) { Person.validates :karma, :unknown => true }
  end

  def test_validates_with_included_validator
    PersonWithValidator.validates :title, :presence => true
    person = PersonWithValidator.new
    person.valid?
    assert_equal ['Local validator'], person.errors[:title]
  end

  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

  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
end