[英]Functional tests of nested routes on Rails 3
I have a Rails 3 app with the following nested route: 我有一个带有以下嵌套路由的Rails 3应用程序:
resources :games do
collection do
get :all
get :unassigned
end
resources :messages
resources :comments
end
A Game has many comments, and a Game also has many messages. 一个游戏有很多注释,一个游戏也有很多消息。
I am expecting that "/games/1/comments" routes to the index action on the comments controller, and sets :game_id => 1
in the params hash. 我期望“ / games / 1 / comments”路由到comments控制器上的index操作,并在params哈希中设置
:game_id => 1
。
Everything is working fine in the app. 在应用程序中一切正常。 However, my route tests are failing and I can't figure out why.
但是,我的路线测试失败了,我不知道为什么。
When I try this: 当我尝试这个:
assert_routing({:path => "/games/1/messages", :method => :get},
{ :controller => "messages", :action => "index", :game_id => 1})
I get this: 我得到这个:
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}>
When I try this (note the quoting on :game_id
) : 当我尝试这样做时(请注意
:game_id
上的引号):
assert_routing({:path => "/games/1/messages", :method => :get},
{ :controller => "messages", :action => "index", :game_id => "1"})
I get this: 我得到这个:
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 <{}>
Also tried this: 还尝试了这个:
assert_routing({:path => "/games/1/messages", :method => :get}, {:controller => "messages", :action => "index"}, {}, {:game_id => "1"})
Response: 响应:
The recognized options <{"action"=>"index", "game_id"=>"1", "controller"=>"messages"}>
did not match <{"action"=>"index", "controller"=>"messages"}>, difference: <{"game_id"=>"1"}>
I think, somehow, I'm getting hung up on the syntax for testing the routing on nested resources. 我认为,以某种方式,我迷上了在嵌套资源上测试路由的语法。 Any ideas?
有任何想法吗?
Thanks in advance-- 提前致谢 -
Assert routing does two things (in that order) 断言路由按此顺序执行两件事
assert_recognizes
assert_generates
So your test-case 2 gets one step further / performs better. 因此,您的测试用例2进一步提高了/性能更好。
Now, assert_generates
checks whether url_for
returns the url you give it. 现在,
assert_generates
检查url_for
是否返回您提供的url。
url_for(:controller => "messages", :action => "index", :game_id => "1")
# should return: /games/1/messages
But according to the exception, it returns /messages?game_id=1
( game_id
as the extra). 但是根据异常,它返回
/messages?game_id=1
(额外的是game_id
)。 This should/can only happen, if you have a resources :messages
rule before your resources :games
. 只有在您的
resources :games
之前有resources :messages
规则时, 才应该/只能发生这种情况。 If that's the case, move it behind, so that the nested rule comes first. 如果是这种情况,请将其移到后面,以便嵌套规则首先出现。
尝试
assert_routing({:path => "/games/1/messages", :method => :get}, {:controller => "messages", :action => "index"}, {}, {:game_id => "1"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.