【发布时间】:2010-12-18 18:05:58
【问题描述】:
我有一个带有以下嵌套路由的 Rails 3 应用程序:
resources :games do
collection do
get :all
get :unassigned
end
resources :messages
resources :comments
end
一个游戏有很多cmets,一个游戏也有很多消息。
我期待“/games/1/cmets”路由到 cmets 控制器上的索引操作,并在 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"}>
我想,不知何故,我对在嵌套资源上测试路由的语法感到困惑。有什么想法吗?
提前致谢--
【问题讨论】:
标签: ruby-on-rails unit-testing routing ruby-on-rails-3