|
1 | 1 | require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2 | 2 |
|
| 3 | +require 'active_support/time' # For testing Date and TimeWithZone objects |
| 4 | + |
3 | 5 | describe "DateValidator" do
|
4 | 6 |
|
5 | 7 | before(:each) do
|
|
8 | 10 |
|
9 | 11 | it "should check validity of the arguments" do
|
10 | 12 | [3, "foo", 1..6].each do |wrong_argument|
|
11 |
| - begin |
| 13 | + expect { |
12 | 14 | TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
|
13 |
| - fail "should not accept a #{wrong_argument.class} as an option (only Time, Date, Symbol or Proc)" |
14 |
| - rescue=>e |
15 |
| - fail e unless e.is_a?(ArgumentError) |
16 |
| - end |
| 15 | + }.to raise_error(ArgumentError, ":before must be a time, a date, a time_with_zone, a symbol or a proc") |
17 | 16 | end
|
18 | 17 | end
|
19 | 18 |
|
20 | 19 | [:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
|
21 | 20 | [:valid,:invalid].each do |should_be|
|
22 | 21 |
|
| 22 | + now = Time.now.to_datetime |
| 23 | + |
23 | 24 | model_date = case check
|
24 |
| - when :after then should_be == :valid ? Time.now + 21000 : Time.now - 1 |
25 |
| - when :before then should_be == :valid ? Time.now - 21000 : Time.now + 1 |
26 |
| - when :after_or_equal_to then should_be == :valid ? Time.now + 21000 : Time.now - 21000 |
27 |
| - when :before_or_equal_to then should_be == :valid ? Time.now - 21000 : Time.now + 21000 |
| 25 | + when :after then should_be == :valid ? now + 21000 : now - 1 |
| 26 | + when :before then should_be == :valid ? now - 21000 : now + 1 |
| 27 | + when :after_or_equal_to then should_be == :valid ? now : now - 21000 |
| 28 | + when :before_or_equal_to then should_be == :valid ? now : now + 21000 |
28 | 29 | end
|
29 | 30 |
|
30 | 31 | it "should ensure that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
|
|
37 | 38 | end
|
38 | 39 |
|
39 | 40 | extra_types = [:proc, :symbol]
|
40 |
| - # extra_types.push(:date) if defined?(Date) and defined?(DateTime) |
| 41 | + extra_types.push(:date) if defined?(Date) and defined?(DateTime) |
| 42 | + extra_types.push(:time_with_zone) if defined?(ActiveSupport::TimeWithZone) |
41 | 43 |
|
42 | 44 | extra_types.each do |type|
|
43 | 45 | it "should accept a #{type} as an argument to a check" do
|
44 | 46 | case type
|
45 | 47 | when :proc then
|
46 |
| - TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}} |
| 48 | + expect { |
| 49 | + TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}} |
| 50 | + }.to_not raise_error |
47 | 51 | when :symbol then
|
48 |
| - begin |
| 52 | + expect { |
49 | 53 | TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
|
50 | 54 | TestRecord.validates :expiration_date, :date => {:after => :min_date}
|
51 |
| - rescue=>e |
52 |
| - fail "is not accepting a #{type} as an argument to a check" |
53 |
| - end |
54 |
| - # when :date then |
55 |
| - # TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date} |
| 55 | + }.to_not raise_error |
| 56 | + when :date then |
| 57 | + expect { |
| 58 | + TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date} |
| 59 | + }.to_not raise_error |
| 60 | + when :time_with_zone then |
| 61 | + expect { |
| 62 | + Time.zone = "Hawaii" |
| 63 | + TestRecord.validates :expiration_date, :date => {:before => Time.zone.parse((Time.now + 21000).to_s)} |
| 64 | + }.to_not raise_error |
56 | 65 | end
|
57 | 66 | end
|
58 | 67 | end
|
59 | 68 |
|
60 | 69 | it "should gracefully handle an unexpected result from a proc argument evaluation" do
|
61 | 70 | TestRecord.validates :expiration_date, :date => {:after => Proc.new{ nil }}
|
62 |
| - TestRecord.new(Time.now).valid?.should be_false |
| 71 | + TestRecord.new(Time.now).should_not be_valid |
63 | 72 | end
|
64 | 73 |
|
65 | 74 | it "should gracefully handle an unexpected result from a symbol argument evaluation" do
|
66 | 75 | TestRecord.send(:define_method, :min_date, lambda { nil })
|
67 | 76 | TestRecord.validates :expiration_date, :date => {:after => :min_date}
|
68 |
| - TestRecord.new(Time.now).valid?.should be_false |
| 77 | + TestRecord.new(Time.now).should_not be_valid |
69 | 78 | end
|
70 | 79 | end
|
0 commit comments