我正在使用Factory Girl在我的模型/單元測試中爲一個組創建兩個實例。我測試的模型來檢查調用根據到期屬性按以下.current僅返回「當前」組......繞過我的模型驗證的工廠女孩創建
describe ".current" do
let!(:current_group) { FactoryGirl.create(:group, :expiry => Time.now + 1.week) }
let!(:expired_group) { FactoryGirl.create(:group, :expiry => Time.now - 3.days) }
specify { Group.current.should == [current_group] }
end
我的問題是,我已經得到驗證模型檢查新組的到期時間是在今天的日期之後。這在下面提出了驗證失敗。
1) Group.current
Failure/Error: let!(:expired_group) { FactoryGirl.create(:group, :expiry => Time.now - 3.days) }
ActiveRecord::RecordInvalid:
Validation failed: Expiry is before todays date
有沒有辦法在使用Factory Girl創建時強制創建組或驗證驗證?
這正是我一直在尋找,謝謝! – Norto23 2012-02-17 03:39:20
查看Jason Denney的答案以獲得更好的解決方案。 – 2017-12-12 18:46:54