我写了一个 rspec 测试,它在我的标题中添加了一些单元。
我有两个模型 => Rubric 和 Units。 Rubrics 有很多单位。 它看起来像这样:
@rubric.units.push Factory :text_unit
@rubric.save
然后找到factory_girl,尝试重写 此代码作为工厂。但它不起作用。
如何在 Factory Girl 中编写此关联。 我试过这个:
factory :common_rubric , :class => :common_info_rubric do |f|
f.sequence(:name) {|n| "common_info_rubric#{n}"}
end
factory :text_unit, :class => text_info_unit do |f|
f.association :common_rubric_with_unit
f.sequence(:name) {|n| "text_unit#n}" }
end
factory :common_rubric_with_unit , :parent => :common_rubric do |f|
f.units { |unit| unit.association(:text_info_unit) }
end
我总是有错误
SystemStackError:
stack level too deep
最佳答案
你在那里有一个循环引用。当您创建一个 text_unit
时,它会创建一个关联的 common_rubric_with_unit
。 common_rubric_with_unit
的定义创建了一个关联的 text_unit
,我们回到了起点。
您需要从任一侧删除其中一个关联,这应该可行:
factory :text_unit, :class => text_info_unit do |f|
f.association :common_rubric_with_unit
f.sequence(:name) {|n| "text_unit#n}" }
end
factory :common_rubric_with_unit , :parent => :common_rubric do |f|
end
关于ruby-on-rails - 工厂女子协会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7024384/