【发布时间】:2011-01-23 19:22:33
【问题描述】:
我有以下型号:
class Person < ActiveRecord::Base
belongs_to :family
end
class Family < ActiveRecord::Base
end
还有以下工厂:
Factory.define :person do |p|
p.association :family
end
Factory.define :family do |f|
end
我正在使用 factory_girl 的 Cucumber 步骤,如下所示:
Given the following people exist:
| First name | Last name |
| John | Doe |
Given the following people exist:
| First name | Last name | Family |
| John | Doe | Name: Doe's family |
我想使用第一种形式创建没有关联家庭的人,而后者则创建有家庭的人。现在每个人都有一个家庭。其实使用第一种形式是失败的,因为我在Family类中也有validates_presence_of :name。
如果我从 Person 工厂定义中删除 p.association :family,后一种形式会失败,因为它试图将字符串设置为关联记录(它运行类似于 family = "Name: ..." 的东西)。
是否可以在 factory_girl 定义的 Cucumber 步骤中具有可选关联?
【问题讨论】:
标签: ruby-on-rails cucumber factory-bot