此问题与何时/如何覆盖在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
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
当限制&#39;被覆盖,显然是因为我没有得到被覆盖的属性:
我尝试使用attributes_for获取属性,我误解了attributes_for以获取传递给&#39;创建组织的值&#39;
FactoryGirl.create(:organization, :limits_attributes => {:gitEnabled => true, :projectsLimit => 3})
鉴于我无法更改组织的after_save中的逻辑,如果用户覆盖限制,我该如何更新限制?事物流中的哪些覆盖应用于FactoryGirl中定义的模型。
我认为如何解释代码的作用可能存在缺陷,或者我想解决的情况与FG的使用方式不符。
感谢。
答案 0 :(得分:0)
我对被覆盖的值不正确。 org具有在保存之前设置的值(覆盖或默认,无论如何)。因此,只需保存副本并重新使用它就可以了。不确定这是否是一个很好的解决方案
org_limits = org.limits.dup
org.save
# Since entitlements are messed up after setting sub-plan set them here.
org.limits.update_attributes(org_limits.attributes)