说我有一个类似
的模型class Vehicle < ActiveRecore::Base
after_initialize :set_ivars
def set_ivars
@my_ivar = true
end
end
在我的代码中的其他地方我做了类似的事情 @ vehicle.instance_variable_set(:@ my_ivar,false)
然后使用此ivar确定运行哪些验证。
如何将此Ivar传递给FactoryGirl?
FactoryGirl.define do
factory :vehicle do
association1
association2
end
end
如何在创建之后,在保存之前将ivar_set编码到上面?
如何将其传递到FactoryGirl.create()
?
答案 0 :(得分:5)
FactoryGirl.define do
factory :vehicle do
association1
association2
ignore do
my_ivar true
end
after(:build) do |model, evaluator|
model.instance_variable_set(:@my_ivar, evaluator.my_ivar)
end
end
end
FactoryGirl.create(:vehicle).my_ivar #=> true
FactoryGirl.create(:vehicle, my_ivar: false).my_ivar #=> false