2014-03-07 26 views
0

我有一些測試失敗,因爲設置factory_girl有困難。我有一個類JOB和一個類模板。還有一個PROVIDER用戶類。作業屬於作業和模板。但是,我目前的工廠分別分配了一個提供者和模板。factory_girl新實例繼承相關類的父ID

這一個遊:

expect { post :create, job: attributes_for(:job, provider_id: provider.id) }.to change(Job,:count).by(1) 

這一個工程(手動定義):

expect { post :create, job: attributes_for(:job, provider_id: provider.id, template_id: create(:template, provider_id: provider.id)) }.to change(Job,:count).by(1) 

有什麼辦法來建立工廠擁有自動創建爲屬於模板供應商?

factory :job do 
    provider 
    template 
    end 

    factory :template do 
    template_type { Faker::Lorem.word } 
    template_text { Faker::Lorem.paragraph(3) } 
    end 

回答

1

從我所瞭解的描述中,模板也應該引用創建的提供者。你可以這樣做寫類似

factory :job do 
    provider 

    after(:build) do |job| 
    job.template = build(:template, provider: job.provider) 
    end 
end 

可以在https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks

+0

這就是我曾經雖然當我通讀文件閱讀。無論如何,它仍然在打破測試...不知道它是否與工廠有關? – Marc

+0

甚至可以調試RSpec:只需在控制器中啓動調試器創建操作並查看它的作用:創建的作業是否有效? job.valid?做這項工作。如果沒有,請檢查那裏有哪些錯誤:job.errors完成這項工作。通常最容易找到爲什麼測試失敗的方法... – Danny

+0

謝謝...在控制器的創建方法中,「@job = Job.new(job_params)」; job_params發送的是provider_id,但不是template_id ...因爲template_id是必需的,所以失敗。應該在工廠之前(:創建)? – Marc

0

瞭解這個對付attributes_for不返回的關聯,你可以定義自己的attributes_for,包括這些孩子的。我曾經寫過一個定製的,非常複雜的方法,包括所有「可訪問」的關聯。我會在這裏包括它,如果你想使用它

# In contrast with "attributes_for", "attributes_for_incl_associations" does return attributes 
# for accessible associations as well 
# Inspired by: http://stackoverflow.com/questions/10290286/factorygirl-why-does-attributes-for-omit-some-attributes 
def attributes_for_incl_associations(*args) 
    obj = FactoryGirl.build(*args); klass = obj.class 

    # Find all accessible attributes in object's class 
    if defined? controller and controller.current_user and klass.active_authorizers.include? controller.current_user.role 
    accessible = klass.accessible_attributes(controller.current_user.role) 
    else 
    accessible = klass.accessible_attributes(:default) 
    end 

    # Find all associations in object's class 
    associations = klass.reflect_on_all_associations(:belongs_to).map { |a| "#{a.name}_id" } 

    # Find all accessible associations in the created object's attributes 
    accessible_associations = obj.attributes.delete_if do |k, v| 
    !accessible.include?(k) || !associations.member?(k) 
    end 

    # Remove all attributes from FactoryGirl attributes that are not accessible 
    factory_attributes = FactoryGirl.attributes_for(*args).delete_if do |k, v| 
    !accessible.include?(k) 
    end 

    # Merge FactoryGirl attributes with accessible associations 
    factory_attributes.merge(accessible_associations.symbolize_keys) 
end