0
我驗證日期字段如下:如何使用早該匹配器來測試日期驗證
驗證:RETURN_DATE,包含:{在:(Date.today + 1.day..Date.today + 4.weeks )}
我測試它使用Rspec的語法如下:
it "should only accept return date that is later than the current date" do
quotation_request = FactoryGirl.build(:quotation_request, return_date: Date.today)
expect(quotation_request.valid?).to be_falsy
end
在報價請求模型驗證:
validates :return_date, inclusion: {in:(Date.today+1.day..Date.today+4.weeks)
這可以工作。我想知道是否有一種方法可以使用Shoulda匹配器來測試,如果有的話,以及如何做到這一點?
我增加了驗證我使用的日期,使我測試更清晰 – chell
我已經更新了答案,包括更好的描述如何解決您遇到的問題@chell – nort
感謝Nort。我試圖使用proc,但它會產生錯誤的參數數量錯誤(1代表0)。這不是建立在Rails日期的工作與proc一樣嗎? – chell