簡體   English   中英

在Rails 3上嵌套路線的功能測試

[英]Functional tests of nested routes on Rails 3

我有一個帶有以下嵌套路由的Rails 3應用程序:

resources :games do
  collection do
    get :all
    get :unassigned
  end
  resources :messages
  resources :comments
end

一個游戲有很多注釋,一個游戲也有很多消息。

我期望“ / games / 1 / comments”路由到comments控制器上的index操作,並在params哈希中設置:game_id => 1

在應用程序中一切正常。 但是,我的路線測試失敗了,我不知道為什么。

當我嘗試這個:

assert_routing({:path => "/games/1/messages", :method => :get},
  { :controller => "messages", :action => "index", :game_id => 1})

我得到這個:

  2) Failure:
test_route_one(MessagesControllerTest)
    [actionpack (3.0.3) lib/action_dispatch/testing/assertions/routing.rb:52:in `assert_recognizes'
     actionpack (3.0.3) lib/action_dispatch/testing/assertions/routing.rb:120:in `assert_routing'
     test/functional/messages_controller_test.rb:106:in `test_route_one'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:67:in `run'
     activesupport (3.0.3) lib/active_support/callbacks.rb:438:in `_run_setup_callbacks'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
The recognized options <{"action"=>"index", "game_id"=>"1", "controller"=>"messages"}> 
did not match <{"action"=>"index", "game_id"=>1, "controller"=>"messages"}>, 
difference: <{"game_id"=>1}>

當我嘗試這樣做時(請注意:game_id上的引號):

assert_routing({:path => "/games/1/messages", :method => :get},
  { :controller => "messages", :action => "index", :game_id => "1"})

我得到這個:

  3) Failure:
test_route_two(MessagesControllerTest)
    [actionpack (3.0.3) lib/action_dispatch/testing/assertions/routing.rb:90:in `assert_generates'
     actionpack (3.0.3) lib/action_dispatch/testing/assertions/routing.rb:127:in `assert_routing'
     test/functional/messages_controller_test.rb:111:in `test_route_two'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:67:in `run'
     activesupport (3.0.3) lib/active_support/callbacks.rb:438:in `_run_setup_callbacks'
     activesupport (3.0.3) lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
found extras <{:game_id=>"1"}>, not <{}>

還嘗試了這個:

assert_routing({:path => "/games/1/messages", :method => :get}, {:controller => "messages", :action => "index"}, {}, {:game_id => "1"})

響應:

The recognized options <{"action"=>"index", "game_id"=>"1", "controller"=>"messages"}> 
did not match <{"action"=>"index", "controller"=>"messages"}>, difference: <{"game_id"=>"1"}>

我認為,以某種方式,我迷上了在嵌套資源上測試路由的語法。 有任何想法嗎?

提前致謝 -

斷言路由按此順序執行兩件事

  • assert_recognizes
  • assert_generates

因此,您的測試用例2進一步提高了/性能更好。

現在, assert_generates檢查url_for是否返回您提供的url。

url_for(:controller => "messages", :action => "index", :game_id => "1")
# should return: /games/1/messages

但是根據異常,它返回/messages?game_id=1 (額外的是game_id )。 只有您的resources :games 之前resources :messages規則時, 應該/只能發生這種情況。 如果是這種情況,請將其移到后面,以便嵌套規則首先出現。

嘗試

assert_routing({:path => "/games/1/messages", :method => :get}, {:controller => "messages", :action => "index"}, {}, {:game_id => "1"})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM