Ruby on rails 为什么使用initialize_with的工厂会导致FactoryGirl::SyntaxRunner错误?
我建立了一个名为costs_estimator的带有初始值设定项的模型,并在上面添加了这个工厂。现在的问题是,当我运行RSpec时,其他测试会报告一个错误:Ruby on rails 为什么使用initialize_with的工厂会导致FactoryGirl::SyntaxRunner错误?,ruby-on-rails,ruby,rspec,ruby-on-rails-3.2,factory-bot,Ruby On Rails,Ruby,Rspec,Ruby On Rails 3.2,Factory Bot,我建立了一个名为costs_estimator的带有初始值设定项的模型,并在上面添加了这个工厂。现在的问题是,当我运行RSpec时,其他测试会报告一个错误: FactoryGirl.define do factory :costs_estimator do ignore do detailed_outcome_estimate nil finance_estimate { detailed_outcome_estimate.try(:finance_es
FactoryGirl.define do
factory :costs_estimator do
ignore do
detailed_outcome_estimate nil
finance_estimate { detailed_outcome_estimate.try(:finance_estimate) }
task_estimators do
if detailed_outcome_estimate.present?
[ build(:task_estimator) ]
end
end
end
end
initialize_with do
new(finance_estimate, task_estimators)
end
end
factory\u girl-4.2.0/lib/factory\u girl/evaluator.rb:42:in'method\u missing':
未定义的“财务估算”方法#
(命名错误)。。。
有人知道这家工厂发生了什么,以及它为什么会影响其他测试吗?如果您需要更多信息,请告诉我。您可能希望
使用
初始化\u仅适用于:成本估算器
工厂。如果是,请将其移动到该定义内:
factory_girl-4.2.0/lib/factory_girl/evaluator.rb:42:in `method_missing':
undefined method `finance_estimate' for #<FactoryGirl::SyntaxRunner:0x00000009c542a8>
(NoMethodError)...
factory_girl确实允许您在顶层定义
初始化
,在这种情况下,它将应用于所有工厂,但这肯定会打破没有定义财务_估算
和任务_估算
的工厂,甚至可能无法使用虚拟属性,因为这些仅在单个工厂的上下文中定义。您真的希望initialize\u with
应用于所有工厂吗?如果没有,在工厂定义内移动它是否有效?是!它神奇地完美工作!谢谢嗯,但是你能告诉我区别吗?我把它写下来作为一个答案,并给出了一个原因和一个可能的原因,为什么上面可能会导致你的问题。
FactoryGirl.define do
factory :costs_estimator do
ignore do
detailed_outcome_estimate nil
finance_estimate { detailed_outcome_estimate.try(:finance_estimate) }
task_estimators do
if detailed_outcome_estimate.present?
[ build(:task_estimator) ]
end
end
end
initialize_with do
new(finance_estimate, task_estimators)
end
end
end