【发布时间】:2014-04-16 18:05:58
【问题描述】:
这个问题与在 FactoryGirl 中覆盖何时/如何工作有关
对于我的具体问题,我有一个具有“限制”属性的组织。 after(:create) 部分设置了一些其他值,最后进行了保存。保存会根据 organization.after_save 方法中的逻辑重置“限制”。所以我不得不做 update_attributes 所以“限制”设置为工厂女孩中定义的任何内容。
这一切正常,除了限制被规范覆盖的情况。
代码如下:
factory :organization do
sequence(:name) {|n| "org#{n + $offset}" }
.....
limits_attributes :projectsLimit => 1, :svnEnabled => true, :deployEnabled => true
...
after(:create) do | org |
sub = FactoryGirl.create(:subscription, :organization => org)
sp = FactoryGirl.create(:subscription_plan, :subscription => sub)
sub.subscription_plans << sp
org.subscription = sub
org.status = :active
org.save
# Since entitlements are messed up after setting sub-plan set them here.
org.limits.update_attributes(:projectsLimit => 1, :svnEnabled => true, :deployEnabled => true)
end
end
当“限制”被覆盖时,这会失败,显然是因为我没有得到被覆盖的属性:
FactoryGirl.create(:organization, :limits_attributes => {:gitEnabled => true, :projectsLimit => 3})
我尝试使用 attributes_for 获取属性,但我误解了 attributes_for 以获取传递给“创建组织”的值
org_attrs = FactoryGirl.attributes_for(:organization)
puts "atts #{org_attrs}"
org.save
# Since entitlements are messed up after setting sub-plan set them here.
org.limits.update_attributes(org_attrs[:limits_attributes])
鉴于我无法更改组织的 after_save 中的逻辑,如果用户覆盖它们,我如何更新限制?在事物的流程中,覆盖应用于 FactoryGirl 中定义的模型。
我认为我解释代码在做什么的方式可能存在缺陷,或者我要解决的方案不适合 FG 的使用方式。
谢谢。
【问题讨论】: