0

기존 프로젝트의 사양을 작성하여 RSpec을 배우고 있습니다. 다형성 리소스에 대한 컨트롤러 사양에 문제가 있습니다. 사실상 다른 모델은 다음과 같이 Notes와 관계를 맺을 수 있습니다. has_many :notes, as: :noteableRSpec : 다형성 리소스가있는 컨트롤러 사양, "경로 일치 없음"오류

또한이 응용 프로그램은 각 사용자가 많은 사용자를 보유 할 수있는 다중 사용자입니다. 각 계정은 URL에 :id 대신 :slug에 의해 액세스됩니다. 그래서 내 mulit 테넌트는 다형성 경로는 다음과 같습니다 시험 문제로 지금에 새로운 액션

new_customer_note GET /:slug/customers/:customer_id/notes/new(.:format)  accounts/notes#new 
new_product_note GET /:slug/products/:product_id/notes/new(.:format)  accounts/notes#new 

을 :

# config/routes.rb 
... 

scope ':slug', module: 'accounts' do 

    ... 

    resources :customers do 
    resources :notes 
    end 

    resources :products do 
    resources :notes 
    end 
end 

이는이 같은 경로가 발생합니다. 첫째, 여기에 내가 invitations_controller 같은 다른 비 다형성 컨트롤러를 테스트하는 방법의 예입니다 : 내가 다형성 notes_controller을 테스트하기 위해 비슷한 방법을 사용하려고하면

# from spec/controllers/accounts/invitation_controller_spec.rb 
require 'rails_helper' 

describe Accounts::InvitationsController do 
    describe 'creating and sending invitation' do 
    before :each do 
     @owner = create(:user) 
     sign_in @owner 
     @account = create(:account, owner: @owner) 
    end 

    describe 'GET #new' do 
     it "assigns a new Invitation to @invitation" do 
     get :new, slug: @account.slug 
     expect(assigns(:invitation)).to be_a_new(Invitation) 
     end 
    end 
    ... 
end 

가, 나는

# from spec/controllers/accounts/notes_controller_spec.rb 

require 'rails_helper' 

describe Accounts::NotesController do 
    before :each do 
    @owner = create(:user) 
    sign_in @owner 
    @account = create(:account, owner: @owner) 
    @noteable = create(:customer, account: @account) 
    end 

    describe 'GET #new' do 
    it 'assigns a new note to @note for the noteable object' do 
     get :new, slug: @account.slug, noteable: @noteable  # no idea how to fix this :-) 
     expect(:note).to be_a_new(:note) 
    end 
    end 
end 
:-) 혼란스러워

여기서는 Before 블록에서 고객을 @noteable로 작성하고 있지만 Product 일 수도 있습니다.

No route matches {:action=>"new", :controller=>"accounts/notes", :noteable=>"1", :slug=>"nicolaswisozk"} 

나는 문제가 무엇인지 볼 수 있지만, 난 그냥 /products/ 또는 /customers/ 같은 URL의 동적 인 부분을 해결하는 방법을 알아낼 수 없습니다 : 나는 RSpec을 실행하면,이 오류가 발생합니다.

업데이트

:-) 어떤 도움도 인정된다

get :new 라인 변경

get :new, slug: @account.slug, customer_id: @noteable 

아래 요청한 이는 오류를 발생

Failure/Error: expect(:note).to be_a_new(:note) 

TypeError: 
    class or module required 
# ./spec/controllers/accounts/notes_controller_spec.rb:16:in `block (3 levels) in <top (required)>' 

그 줄의 16 번 줄 C는 다음과 같습니다

expect(:note).to be_a_new(:note) 

때문일 수 있습니다 :이

def new 
    @noteable = find_noteable 
    @note = @noteable.notes.new 
end 

답변

2
을? :처럼 내 notes_controller.rb의 새로운 조치가 단지 @note = Note.new 아니지만, @noteable에 새로운 주를 초기화

그럼 여기서 문제는 PARAMS에 :noteable을 전달하는이 라인

get :new, slug: @account.slug, noteable: @noteable 

에서 그이어야합니다. 하지만 레일의 모든 동적 부분을 전달해야 레일이 경로와 일치하는 데 도움이됩니다. 여기서 :customer_id을 매개 변수로 전달해야합니다. 이와 같이

get :new, slug: @account.slug, customer_id: @noteable.id 

이 정보가 도움이되는지 알려주세요.

+0

감사합니다. 뭔가있는 것처럼 보입니다. 라우팅 오류가 사라졌지만 이제는 'TypeError : class or module required'라고 표시됩니다.나는 그것이 Customer 클래스를 참조하고 있다고 생각한다. 어떻게 테스트에 포함시킬 수 있습니까? – GreyEyes

+0

오류에 대한 스택 추적을 제공 할 수 있습니까? 그게 도움이 될거야. –

+0

더 나은 형식 지정을 위해 질문에 포함하십시오. –