0

在我的 rails 3.2.3 应用程序中,我有一个主题控制器,它被建模为资源。我想编写一个功能测试来验证 /topics 上的帖子是有效路线。这应该首先失败,然后我将添加代码来修复它。但是,我在路由测试中遇到错误,而不是失败。我在做什么错?(注意:如果我在 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
4

1 回答 1

1

创建routes.rb的路线与您的测试路线不同。如果你想路由到:create控制器中的动作,routes.rb你应该使用:

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

请参阅 RailsGuides 中的路由主题

于 2012-09-22T19:30:25.987 回答