1
- require 'spec_helper '
1
+ require 'test_helper '
2
2
3
3
module ActiveModel
4
4
module Validations
@@ -11,10 +11,9 @@ module Validations
11
11
12
12
it "checks validity of the arguments" do
13
13
[ 3 , "foo" , 1 ..6 ] . each do |wrong_argument |
14
- expect {
15
- TestRecord . validates :expiration_date ,
16
- :date => { :before => wrong_argument }
17
- } . to raise_error ( ArgumentError , ":before must be a time, a date, a time_with_zone, a symbol or a proc" )
14
+ proc {
15
+ TestRecord . validates ( :expiration_date , :date => { :before => wrong_argument } )
16
+ } . must_raise ( ArgumentError , ":before must be a time, a date, a time_with_zone, a symbol or a proc" )
18
17
end
19
18
end
20
19
@@ -23,38 +22,37 @@ module Validations
23
22
:date => { :before => Time . now }
24
23
25
24
model = TestRecord . new ( nil )
26
- model . should_not be_valid
27
- model . errors [ :expiration_date ] . should eq ( [ "is not a date" ] )
25
+ model . valid? . must_equal false
26
+ model . errors [ :expiration_date ] . must_equal ( [ "is not a date" ] )
28
27
end
29
28
30
29
it "works with helper methods" do
31
30
time = Time . now
32
31
TestRecord . validates_date_of :expiration_date , :before => time
33
32
model = TestRecord . new ( time + 20000 )
34
- model . should_not be_valid
33
+ model . valid? . must_equal false
35
34
end
36
35
37
- [ :valid , :invalid ] . each do |should_be |
38
- _context = should_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
36
+ [ :valid , :invalid ] . each do |must_be |
37
+ _context = must_be == :valid ? 'when value validates correctly' : 'when value does not match validation requirements'
39
38
40
- context _context do
39
+ describe _context do
41
40
[ :after , :before , :after_or_equal_to , :before_or_equal_to ] . each do |check |
42
41
now = Time . now . to_datetime
43
42
44
43
model_date = case check
45
- when :after then should_be == :valid ? now + 21000 : now - 1
46
- when :before then should_be == :valid ? now - 21000 : now + 1
47
- when :after_or_equal_to then should_be == :valid ? now : now - 21000
48
- when :before_or_equal_to then should_be == :valid ? now : now + 21000
44
+ when :after then must_be == :valid ? now + 21000 : now - 1
45
+ when :before then must_be == :valid ? now - 21000 : now + 1
46
+ when :after_or_equal_to then must_be == :valid ? now : now - 21000
47
+ when :before_or_equal_to then must_be == :valid ? now : now + 21000
49
48
end
50
49
51
- it "ensures that an attribute is #{ should_be } when #{ should_be == :valid ? 'respecting' : 'offending' } the #{ check } check" do
50
+ it "ensures that an attribute is #{ must_be } when #{ must_be == :valid ? 'respecting' : 'offending' } the #{ check } check" do
52
51
TestRecord . validates :expiration_date ,
53
52
:date => { :"#{ check } " => now }
54
53
55
54
model = TestRecord . new ( model_date )
56
- should_be == :valid ? model . should ( be_valid , "an attribute should be valid when respecting the #{ check } check" ) \
57
- : model . should_not ( be_valid , "an attribute should be invalidwhen offending the #{ check } check" )
55
+ must_be == :valid ? model . valid? . must_equal ( true ) : model . valid? . must_equal ( false )
58
56
end
59
57
60
58
if _context == 'when value does not match validation requirements'
@@ -63,8 +61,8 @@ module Validations
63
61
:date => { :"#{ check } " => now }
64
62
65
63
model = TestRecord . new ( model_date )
66
- model . should_not be_valid
67
- model . errors [ :expiration_date ] . should eq ( [ "must be " + check . to_s . gsub ( '_' , ' ' ) + " #{ I18n . localize ( now ) } " ] )
64
+ model . valid? . must_equal false
65
+ model . errors [ :expiration_date ] . must_equal ( [ "must be " + check . to_s . gsub ( '_' , ' ' ) + " #{ I18n . localize ( now ) } " ] )
68
66
end
69
67
end
70
68
end
@@ -78,8 +76,8 @@ module Validations
78
76
:message => 'must be after Christmas' }
79
77
80
78
model = TestRecord . new ( now + 21000 )
81
- model . should_not be_valid
82
- model . errors [ :expiration_date ] . should eq ( [ "must be after Christmas" ] )
79
+ model . valid? . must_equal false
80
+ model . errors [ :expiration_date ] . must_equal ( [ "must be after Christmas" ] )
83
81
end
84
82
85
83
it "allows custom validation message to be handled by I18n" do
@@ -89,8 +87,8 @@ module Validations
89
87
TestRecord . validates :expiration_date , :date => true
90
88
91
89
model = TestRecord . new ( nil )
92
- model . should_not be_valid
93
- model . errors [ :expiration_date ] . should eq ( [ custom_message ] )
90
+ model . valid? . must_equal false
91
+ model . errors [ :expiration_date ] . must_equal ( [ custom_message ] )
94
92
end
95
93
end
96
94
@@ -105,24 +103,15 @@ module Validations
105
103
it "accepts a #{ type } as an argument to a check" do
106
104
case type
107
105
when :proc then
108
- expect {
109
- TestRecord . validates :expiration_date ,
110
- :date => { :after => Proc . new { Time . now + 21000 } }
111
- } . to_not raise_error
106
+ TestRecord . validates ( :expiration_date , :date => { :after => Proc . new { Time . now + 21000 } } ) . must_be_kind_of Hash
112
107
when :symbol then
113
- expect {
114
- TestRecord . send ( :define_method , :min_date , lambda { Time . now + 21000 } )
115
- TestRecord . validates :expiration_date , :date => { :after => :min_date }
116
- } . to_not raise_error
108
+ TestRecord . send ( :define_method , :min_date , lambda { Time . now + 21000 } )
109
+ TestRecord . validates ( :expiration_date , :date => { :after => :min_date } ) . must_be_kind_of Hash
117
110
when :date then
118
- expect {
119
- TestRecord . validates :expiration_date , :date => { :after => Time . now . to_date }
120
- } . to_not raise_error
111
+ TestRecord . validates ( :expiration_date , :date => { :after => Time . now . to_date } ) . must_be_kind_of Hash
121
112
when :time_with_zone then
122
- expect {
123
- Time . zone = "Hawaii"
124
- TestRecord . validates :expiration_date , :date => { :before => Time . zone . parse ( ( Time . now + 21000 ) . to_s ) }
125
- } . to_not raise_error
113
+ Time . zone = "Hawaii"
114
+ TestRecord . validates ( :expiration_date , :date => { :before => Time . zone . parse ( ( Time . now + 21000 ) . to_s ) } ) . must_be_kind_of Hash
126
115
end
127
116
end
128
117
end
@@ -131,15 +120,15 @@ module Validations
131
120
TestRecord . validates :expiration_date ,
132
121
:date => { :after => Proc . new { nil } }
133
122
134
- TestRecord . new ( Time . now ) . should_not be_valid
123
+ TestRecord . new ( Time . now ) . valid? . must_equal false
135
124
end
136
125
137
126
it "gracefully handles an unexpected result from a symbol argument evaluation" do
138
127
TestRecord . send ( :define_method , :min_date , lambda { nil } )
139
128
TestRecord . validates :expiration_date ,
140
129
:date => { :after => :min_date }
141
130
142
- TestRecord . new ( Time . now ) . should_not be_valid
131
+ TestRecord . new ( Time . now ) . valid? . must_equal false
143
132
end
144
133
end
145
134
0 commit comments