在我的事件模型中,我(曾经有)这些行用于验证:
validates_date_of :begins_at
validates_date_of :deadline
validates :deadline, :date => {:before_or_equal_to => :begins_at}
现在我想拥有两个字段选项,以满足“未确定”事件。但是,如果我有
validates_date_of :begins_at, :allow_nil => true
validates_date_of :deadline, :allow_nil => true
validates :deadline, :date => {:before_or_equal_to => :begins_at }, :allow_nil => true
这些测试失败(感谢leemachin的建议,我重构了)
it "should accept nil as begining_date" do
@event.begins_at=nil;
@event.should be_valid
end
it "should have valid deadline" do
@event.deadline="Not a date";
@event.should_not be_valid
end
而这些通过
it "should accept both dates as nil" do
@event.begins_at=nil;
@event.deadline=nil;
@event.should be_valid
end
it "should accept nil as deadline" do
@event.deadline=nil;
@event.should be_valid
end
我在程序逻辑中有一个解决方法,只要开始日期为零,就将截止日期设置为零,但测试仍然失败。有任何想法吗?
谢谢
PS: 使用 gem "date_validator", "~> 0.6.3"*