问题
I have nested resources:
resources :requests do
resources :responses
end
and want to write controller test for response model. When I try to write:
RSpec.describe ResponsesController, type: :controller do
describe 'GET #show' do
before do
@testrequest = Request.create
@testresponse = @testrequest.responses.create
get :show, request_id: @testrequest.id, id: @testresponse.id
end
it 'show specific response selected by id if user owner of response or user owner of parent request' do
expect(assigns(:response)).to eq @testresponse
end
end
I got error:
ResponsesController GET #show show specific response selected by id if user owner of response or user owner of parent request
Failure/Error: get :show, request_id: @testrequest.id, id: @testresponse.id
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"responses", :id=>"1", :request_id=>"1"}
What I doing wrong?
I trying to write:
get :show, request_id: @testrequest, response_id: @testresponse
get :show, request_id: @testrequest.id, response_id: @testresponse.id
get "/requests/#{@testrequest.id}/responses/#{@testresponse.id}"
get :show, "/requests/#{@testrequest.id}/responses/#{@testresponse.id}"
get :show, request_response_path(@testrequest, @testresponse)
with same error.
UPDATE
Right answer is:
get :show, id: @testresponse.id, request_id: @testrequest.id
来源:https://stackoverflow.com/questions/33678450/rails-rspec-controller-testing-with-nested-resources