工厂女孩创建绕过我的模型验证 [英] Factory-girl create that bypasses my model validation
问题描述
我正在使用Factory Girl在我的模型/单元测试中为一个组创建两个实例.我正在测试模型,以检查对.current的调用是否根据如下所述的expiry属性仅返回当前"组...
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...
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
使用Factory Girl创建时,是否可以强制创建组或解决验证问题?
Is there a way to forcefully create the Group or get around the validation when creating using Factory Girl?
推荐答案
这不是FactoryGirl特有的,但是通过save(:validate => false)
保存模型时,您始终可以绕过验证:
This isn't very specific to FactoryGirl, but you can always bypass validations when saving models via save(:validate => false)
:
describe ".current" do
let!(:current_group) { FactoryGirl.create(:group) }
let!(:old_group) {
g = FactoryGirl.build(:group, :expiry => Time.now - 3.days)
g.save(:validate => false)
g
}
specify { Group.current.should == [current_group] }
end
这篇关于工厂女孩创建绕过我的模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!