2012-04-11 94 views
0

在我的轨道3.2.3应用程序,我有一个主题控制器,它被建模为资源。我想编写一个功能测试来验证帖子上/主题是否是有效的路线。这应该首先失败,然后我将添加代码来修复它。但是,我在路由测试中遇到错误,而不是失败。我在做什么错误(注:如果我修复routes.rb中的路线,测试通过 - 只是不知道为什么,我在测试中得到一个错误,而不是一个失败的):assert_routing与方法给出了一个错误,而不是失败

# 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

所创建的路线在routes.rb是从路径的测试不同。如果你想路由到控制器中的:create行动,在你的routes.rb你应该使用:

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

routing topic在RailsGuides。

相关问题