Abr*_*m P 5 ruby mocking ruby-on-rails-3.2 factory-bot
说我有一个像这样的模特
class Vehicle < ActiveRecore::Base
after_initialize :set_ivars
def set_ivars
@my_ivar = true
end
end
Run Code Online (Sandbox Code Playgroud)
我的代码中的其他地方我做的事情就像@ vehicle.instance_variable_set(:@ my_ivar,false)
然后使用此ivar确定运行哪些验证.
如何将这个Ivar传递给FactoryGirl?
FactoryGirl.define do
factory :vehicle do
association1
association2
end
end
Run Code Online (Sandbox Code Playgroud)
在创建之后,如何在保存之前将ivar_set编码到上面?我如何将其传递给FactoryGirl.create()?
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
Run Code Online (Sandbox Code Playgroud)