2
votes

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
You have to use plural form of the recource name in routes: resources :requests instead of resources :request. Again nested resource name should in plural form responses - andrykonchin
Thanks. Error in text. Actually I used plural form. - Denis Savchuk
the order of hash arguments (:id, :request_id) plays no role, so there should be no difference between your original call and the one in the update. - bosskovic
For me only last version working fine. - Denis Savchuk