[英]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.