1- require 'spec_helper '
1+ require 'test_helper '
22
33module ActiveModel
44 module Validations
@@ -11,10 +11,9 @@ module Validations
1111
1212 it "checks validity of the arguments" do
1313 [ 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" )
1817 end
1918 end
2019
@@ -23,38 +22,37 @@ module Validations
2322 :date => { :before => Time . now }
2423
2524 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" ] )
2827 end
2928
3029 it "works with helper methods" do
3130 time = Time . now
3231 TestRecord . validates_date_of :expiration_date , :before => time
3332 model = TestRecord . new ( time + 20000 )
34- model . should_not be_valid
33+ model . valid? . must_equal false
3534 end
3635
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'
3938
40- context _context do
39+ describe _context do
4140 [ :after , :before , :after_or_equal_to , :before_or_equal_to ] . each do |check |
4241 now = Time . now . to_datetime
4342
4443 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
4948 end
5049
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
5251 TestRecord . validates :expiration_date ,
5352 :date => { :"#{ check } " => now }
5453
5554 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 )
5856 end
5957
6058 if _context == 'when value does not match validation requirements'
@@ -63,8 +61,8 @@ module Validations
6361 :date => { :"#{ check } " => now }
6462
6563 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 ) } " ] )
6866 end
6967 end
7068 end
@@ -78,8 +76,8 @@ module Validations
7876 :message => 'must be after Christmas' }
7977
8078 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" ] )
8381 end
8482
8583 it "allows custom validation message to be handled by I18n" do
@@ -89,8 +87,8 @@ module Validations
8987 TestRecord . validates :expiration_date , :date => true
9088
9189 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 ] )
9492 end
9593 end
9694
@@ -105,24 +103,15 @@ module Validations
105103 it "accepts a #{ type } as an argument to a check" do
106104 case type
107105 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
112107 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
117110 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
121112 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
126115 end
127116 end
128117 end
@@ -131,15 +120,15 @@ module Validations
131120 TestRecord . validates :expiration_date ,
132121 :date => { :after => Proc . new { nil } }
133122
134- TestRecord . new ( Time . now ) . should_not be_valid
123+ TestRecord . new ( Time . now ) . valid? . must_equal false
135124 end
136125
137126 it "gracefully handles an unexpected result from a symbol argument evaluation" do
138127 TestRecord . send ( :define_method , :min_date , lambda { nil } )
139128 TestRecord . validates :expiration_date ,
140129 :date => { :after => :min_date }
141130
142- TestRecord . new ( Time . now ) . should_not be_valid
131+ TestRecord . new ( Time . now ) . valid? . must_equal false
143132 end
144133 end
145134
0 commit comments