作者:小布丁她Daddy | 来源:互联网 | 2023-08-20 09:45
IamusingFactoryGirltocreatetwoinstancesinmymodelunittestforaGroup.Iamtestingthe
I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the 'current' groups according to the expiry attribute as per below...
我正在使用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
My problem is that I've got validation in the model that checks a new group's expiry is after today's date. This raises the validation failure below.
我的问题是,我在模型中得到了验证,它检查一个新组的过期日期是在今天之后。这将导致下面的验证失败。
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
Is there a way to forcefully create the Group or get around the validation when creating using Factory Girl?
在使用Factory Girl创建时,是否有办法强制创建组或绕过验证?
7 个解决方案