2013-10-11 102 views
1

我查看了在線手冊,但我仍不確定如何在Factory Girl中正確設置關聯。我有一個課程,它有許多級別有許多步驟(級別屬於課程和步驟屬於級別)在Factory Girl中設置關聯 - 三個has_many&belongs_to嵌套關係

這是我得到的錯誤 - 它基本上傳遞了整個步驟,而不僅僅是course_id,level_id和(步驟)編號。

No route matches {:action=>"show", :controller=>"steps", :course_id=>#<Step id: 1, 
description: "Description for course 1", cell_location: nil, mc_answer: nil, level_id: 
nil, created_at: "2013-10-11 16:03:11", updated_at: "2013-10-11 16:03:11", media_type: 
nil, video_link: nil, choice_one: nil, choice_two: nil} 

這裏是我的工廠 - 我相信我需要以某種方式創建步驟,其水平和課程內

FactoryGirl.define do 
     factory :user do 
     sequence(:name) { |n| "Person #{n}" } 
     sequence(:email) { |n| "person_#{n}@example.com" } 
     password "foobar" 
     password_confirmation "foobar" 

     factory :admin do 
      admin true 
     end 
     end 

     factory :course do 
     sequence(:title) { |n| "Ze Finance Course #{n}" } 
     sequence(:description) { |n| "Description for course #{n}" } 
     end 

     factory :level do 
     sequence(:title) { |n| "Ze Finance Level #{n}" } 
     end 

     factory :step do 
     sequence(:description) { |n| "Description for course #{n}" } 
     end 


end  

這裏是RSpec的測試:

describe "attempting a step" do 
     let(:step) { FactoryGirl.create(:step)} 
     before { sign_in user} 

     describe "taking a course" do 
      before { visit course_level_step_path(step)} <<< here is where it goes wrong 

      it "should increment the user step count" do 
       expect do 
        click_button "check answer" 
       end.to change(user.user_steps, :count).by(1) 
      end 

      describe "toggling the button" do 
       before { click_button "check answer"} 
       it { should have_selector('input', value: 'remove step')} 
      end 

     end 

回答

1

您可以定義像這樣的協會:

factory :step do 
    level 
    sequence(:description) { |n| "Description for course #{n}" } 
end 

我假設你在步模型中有belongs_to :level

此外,錯誤實際上不是工廠女孩的錯誤。這是一個路由錯誤,那麼你如何定義你的路由?

+0

http://stackoverflow.com/questions/19324358/factory-girl-rspec-gives-follow-error-actionviewtemplateerror-object它似乎是一個路由錯誤,但這個答案幫助我與關聯問題。開始的鏈接有一個更加擴展的路由問題版本,你建議我可以使用幫助。 –

相關問題