2014-09-04 74 views
0

我有以下FactoryGirl結構:我的FactoryGirl關聯屬性在哪裏?

trait :basic_asset_attributes do 
    association :organization, :factory => :transit_agency 
end 

trait :vehicle_attributes do 
    basic_asset_attributes 
    vin "1FMDU34E5XZ464008" 
end 

factory :bus, :class => :vehicle do 
    vehicle_attributes 
    seating_capacity 40 
end 

organization是建立像這樣:

factory :organization do 
    customer_id 1 
    address1 '123 Fake St' 
    city 'Fakesville' 
    state 'PA' 
    zip '17120' 
    url 'http://www.example.com' 
    phone '9999999999' 
    sequence(:name) { |n| "Org #{n}" } 

    factory :transit_agency, class: TransitAgency do 
    end 
end 

FactoryGirl.build(:transit_agency).valid?回報true

然而,當我打電話`FactoryGirl.attributes_for(:公交車),我得到以下內容:

{:vin=>"1FMDU34E5XZ464008", 
:seating_capacity=>40, 
} 

我的組織關聯發生了什麼?這是有效的,所以爲什麼不是

+0

的可能重複[FactoryGirl:爲什麼文件屬性\ _for省略一些屬性(http://stackoverflow.com/questions/10290286/ factorygirl - 爲什麼 - 不 - 屬性換省略-一些特性)。 – ABMagil 2014-09-24 21:51:49

回答

0

我發現here這是設計。

從這個問題:

因爲處理不同的嵌套和各種情況,與普通的用例(控制器規格)以及經常想直接指定FKS的複雜性,attributes_for不處理協會

以爲他們繼續說:

雖然周邊管理協會和複雜數據,這並不意味着你運氣不好;您可以編寫自己的 策略(或修改FactoryGirl's),以獲得所需的功能。

要覆蓋FactoryGirl的attributes_for,你可以這樣做:

# spec/support/factory_girl/custom_attributes_for.rb 
class CustomAttributesFor 
    def association(runner) 
    runner.run(:create) 
    end 

    def result(evaluation) 
    evaluation.object.attributes # this assumes your instance responds to attributes and returns a hash, which AR::Base subclasses will do 
    end 
end 

FactoryGirl.register_strategy(:attributes_for, CustomAttributesFor) 
相關問題