Rails 3上嵌套路由的功能测试

时间:2010-12-18 18:05:58

标签: ruby-on-rails unit-testing routing ruby-on-rails-3

我有一个带有以下嵌套路径的Rails 3应用程序:

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

游戏有很多评论,游戏也有很多消息。

我希望“/ games / 1 / comments”路由到评论控制器上的索引操作,并在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"}>

我想,不知怎的,我已经开始研究在嵌套资源上测试路由的语法了。有什么想法吗?

提前致谢 -

2 个答案:

答案 0 :(得分:5)

断言路由会做两件事(按此顺序)

  • assert_recognizes
  • assert_generates

因此,您的测试用例2更进一步/表现更好。

现在,assert_generates会检查url_for是否返回您提供的网址。

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

但是根据异常,它返回/messages?game_id=1game_id作为额外的)。如果您在<{em> resources :messages之前有resources :games规则,那么这应该/只能发生。如果是这种情况,请将其移至后面,以便嵌套规则首先出现。

答案 1 :(得分:0)

尝试

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