2014-03-07 1 views
0

factory_girl 설정이 어려워 일부 테스트가 실패했습니다. 나는 직업 JOB과 수업 TEMPLATE을 가지고있다. PROVIDER 사용자 클래스도 있습니다. 작업은 JOB과 TEMPLATE 모두에 속합니다. 그러나 현재의 공장에서는 공급자와 템플리트를 따로 지정합니다.factory_girl 새 인스턴스가 연결된 클래스의 부모 ID를 상속합니다

THIS ONE 나누기 :

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

귀하의 설명에서 알 수 있듯이 템플릿은 생성 된 제공 업체를 참조해야합니다. 당신은 당신이 attributes_for하지 반환 협회에 대처하기 위해 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

이것에 대해 읽을 수

factory :job do 
    provider 

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

등이 작성하는 일을 할 수있는, 당신은뿐만 아니라 이들을 포함, 자신의 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 
관련 문제