assert_routing with method正在给出错误而不是失败

时间:2012-04-11 08:41:53

标签: ruby-on-rails-3 routes functional-testing assertions

在我的rails 3.2.3 app中,我有一个主题控制器,它被建模为资源。我想写一个功能测试来验证帖子/主题是一个有效的路线。这应该首先失败,然后我将添加代码来修复它。但是,我在路由测试中遇到错误,而不是失败。我做错了什么?(注意:如果我在routes.rb中修复路由,测试通过 - 只是不确定为什么我收到错误而不是测试失败):

# topics_controller_test.rb

test 'route exists to create topic' do
  assert_routing({:path => '/topics', :method => 'post'} , { :controller => "topics", :action => "create"}, {}, {}, 'could not route to create topic')
end

# routes.rb

resources :topics, :only => [:new]

# terminal output

$ rake test:functionals
Run options: 

# Running tests:

.....E.

Finished tests in 0.373543s, 18.7395 tests/s, 53.5414 assertions/s.

1) Error:
test_route_exists_to_create_topic(TopicsControllerTest):
ActionController::RoutingError: No route matches "/topics"
.../gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:633:in `recognize_path'
.../gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/testing/assertions/routing.rb:210:in `recognized_request_for'
.../gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/testing/assertions/routing.rb:42:in `assert_recognizes'
.../gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/testing/assertions/routing.rb:118:in `assert_routing'
        `.../myapp/test/functional/topics_controller_test.rb:25:in block in <class:TopicsControllerTest>'`

>> 7 tests, 20 assertions, 0 failures, 1 errors, 0 skips

1 个答案:

答案 0 :(得分:1)

routes.rb中创建的路线与您的测试路线不同。如果您想要路由到控制器中的:create操作,请在routes.rb中使用:

resources :topics, :only => [:create]

请参阅RailsGuides中的routing topic