我通常使用此步骤与 factory_girl 设置记录:
Given /^the following (.+) records?:$/ do |factory, table|
table.hashes.each do |hash|
Factory(factory, hash)
end
end
这是我在设置关联时的解决方法:
Given the following group record:
| id | name |
| 1 | foo |
And the following item records:
| name | group_id |
| bar | 1 |
| baz | 1 |
# ...
我知道这很糟糕。从域人的角度来看,使用 id 会使整个事情变得脆弱和神秘。
所以,我的问题是——与 factory_girl 建立关联以及像上面那样的表参数的最佳实践是什么?
最佳答案
您可以在一个工厂中定义多个关联。
像下面这样:
Factory.define :item do |item|
item.name "item_name"
end
Factory.define :group do |group|
group.name "group_name"
group.items { |items| [ items.association(:item), items.association(:item) ] }
end
做一个
Factory(:group)
将创建包含两个项目的组 uplet。
关于ruby-on-rails - 在 cucumber 中与 factory_girl 建立关联的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1673533/