Rails4测试嵌套资源与多个URL参数 - Rails 4 testing nested resources with multiple URL params

- 此内容更新于:2016-02-01
主题:

我一直在这几个小时,仍然不能算出来。我有2测试2的行为在一个嵌套资源控制器。父资源的路线,路线的嵌套资源。这两个测试给我一个错误。没有意义。在第一个测试中,它试图采取行动而不是运行。这是我测试:这是错误:

原文:

I have been at this for a few hours, still can't figure it out. I have 2 tests on 2 actions on a nested resources controller. requests is the parent resources route, and response is the nested resources route.

These 2 tests give me a no route matches error. Does not make sense. In the first test, it tries to run the update action instead of the edit. Here are my tests:

test "should get edit" do
  assert_routing edit_request_response_path(@myresponse.request_id, @myresponse), { :controller => "responses", :action => "edit", :request_id => @myresponse.request_id.to_s, :id => @myresponse.id.to_s }
  get :edit, params: { id: @myresponse, request_id: @myresponse.request_id }
  assert_response :success
end

test "should update response" do
  post :update, :request_id => @myresponse.request_id, response: { body: @myresponse.body, request_id: @myresponse.request_id, status: @myresponse.status, subject: @myresponse.subject, user_id: @myresponse.user_id }
  assert_redirected_to response_path(assigns(:response))
end

Here are the errors:

3) Error:
ResponsesControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"responses", :params=>{:id=>"980190962", :request_id=>"999788447"}}
test/controllers/responses_controller_test.rb:43:in `block in <class:ResponsesControllerTest>'


4) Error:
ResponsesControllerTest#test_should_update_response:
ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"responses", :request_id=>"999788447", :response=>{:body=>"This is the body", :request_id=>"999788447", :status=>"draft", :subject=>"This is the subject", :user_id=>"175178709"}}
test/controllers/responses_controller_test.rb:48:in `block in <class:ResponsesControllerTest>'
解决方案:
在这种情况下,您可能希望使用浅嵌套自没有理由走如果你能得到的响应。然而http://guides.rubyonrails.org/routing.htmlnested-resources命名您的业务逻辑对象和是一个大misstake。这些已经在Rails中关键概念对应于来自客户机的请求和响应发送到客户机Rails。你最终会困惑自己和任何工作项目的可怜虫。加上你最终会屏蔽的要求和方法ActionControllerAPI的非常重要的部分。使用其他一些同义词。
原文:

In this case you might want to use shallow nesting since there is no reason to go through request if you can get to a response by /response/:id.

resources :requests, shallow: true do
  resources :response
end

test "should get edit" do
  assert_routing edit_response_path(@myresponse), { :controller => "responses", :action => "edit", :id => @myresponse.id.to_s }
  get :edit, params: { id: @myresponse, request_id: @myresponse.request_id }
  assert_response :success
end

However naming your business logic objects Request and Response is a big misstake. These are already key concepts in Rails which correspond to the request from the client and the response sent to the client by rails.

You will end up confusing yourself and any poor sucker that has to work on the project. Plus you will end up masking the request and response methods that are pretty important parts of the ActionController API.

Use some other synonym instead.