Rails Rspec controller testing with nested resources

岁酱吖の 提交于 2021-01-28 04:48:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!