在rails中测试命名路由

时间:2010-09-02 20:09:56

标签: ruby-on-rails

我在routes.rb中定义了以下路由:

map.search 'fuzzyleads/search/:url', :controller => 'fuzzyleads', :action => 'search', :method => 'get'

问题是我无法通过此测试:

def test_search_route
  assert_generates "fuzzyleads/search/someurl", { :controller => "fuzzyleads", :action => "search", :url => "someurl" }
end

它不喜欢url部分,我收到以下错误:

  

发现额外内容< {:url =>“someurl”}>而不是   < {}>

我不知道为什么,谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

rake routes的输出会帮助我们做出更好的猜测。以下是我使用rails-2.3.8进行测试的方法:

(1)在config/routes.rb

中添加了您的路线作为第二条路线

(2)创建了以下rspec测试:

it "should have valid route for fuzzyleads search" do
  route_for(:controller => 'fuzzyleads', :action => 'search', :url => 'someurl').should ==
    "/fuzzyleads/search/someurl"
end

但这失败了以下内容:

ActionController::RoutingError in 'FuzzyLeadsController should have valid route for fuzzyleads search'                   
No route matches "/fuzzyleads/search/someurl" with {:method=>:get}                                                  
If you're expecting this failure, we suggest {:get=>"/fuzzyleads/search/someurl"}.should_not be_routable 

但是当我删除:method限制时,一切都成功了。添加:method => 'get'不足以使测试通过。当我在未满足的路线上设置条件时,我已经看到类似的失败,所以我猜你的情况会发生类似的情况。