Rails使用嵌套资源进行Rspec控制器测试

时间:2015-11-12 18:15:15

标签: ruby-on-rails ruby rspec tdd bdd

我有嵌套资源:

  resources :requests do
    resources :responses
  end

并且想要为响应模型编写控制器测试。 当我试着写:

    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

我收到了错误:

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"}

我做错了什么?

我试着写:

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)

同样的错误。

更新

正确答案是:

get :show, id: @testresponse.id, request_id: @testrequest.id

0 个答案:

没有答案
相关问题