Rails 4使用多个URL参数测试嵌套资源

时间:2016-02-01 05:25:17

标签: ruby-on-rails ruby unit-testing ruby-on-rails-4 testing

我已经在这几个小时了,仍然无法弄明白。我对嵌套资源控制器上的2个操作进行了2次测试。 response是父资源路由,no route matches是嵌套资源路由。

这两个测试给出了update错误。没有意义。在第一个测试中,它会尝试运行edit操作而不是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 。以下是我的测试:

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>'

以下是错误:

$path = "H:\oim\adcbsm007\adcbsm007.txt"
$path = "H:\oim\alps027\alps027.txt"
$word = "2016,01,28"
$replacement = "2016,01,29"
$text = get-content $path 
$newText = $text -replace $word,$replacement
$newText > $path
$path = "H:\oim\adcbm007\adcsm007.txt"
$path = "H:\oim\alps027\alps027.txt"
$word = "2016,01,29"
$replacement = "2016,01,30"
$text = get-content $path 
$newText = $text -replace $word,$replacement
$newText > $path

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可能希望使用浅嵌套,因为如果您可以通过request获得响应,则没有理由通过/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

但是,命名业务逻辑对象RequestResponse是一个很大的错误。这些已经是Rails中的关键概念,它们对应于来自客户端的请求以及通过rails发送给客户端的响应。

你最终会混淆自己和任何必须参与该项目的可怜的傻瓜。此外,您最终将屏蔽requestresponse方法,这些方法是ActionController API非常重要的部分。

改为使用其他同义词。