2010-06-17 29 views
1

我在我的應用程序以下路線:Rails的功能測試上的自定義路徑

     GET /admin/comments(.:format)     {:controller=>"admin/comments", :action=>"index"} 
     admin_comments POST /admin/comments(.:format)     {:controller=>"admin/comments", :action=>"create"} 
    new_admin_comment GET /admin/comments/new(.:format)    {:controller=>"admin/comments", :action=>"new"} 
         GET /admin/comments/:id(.:format)    {:controller=>"admin/comments", :action=>"show"} 
         PUT /admin/comments/:id(.:format)    {:controller=>"admin/comments", :action=>"update"} 
     admin_comment DELETE /admin/comments/:id(.:format)    {:controller=>"admin/comments", :action=>"destroy"} 
    edit_admin_comment GET /admin/comments/:id/edit(.:format)  {:controller=>"admin/comments", :action=>"edit"} 
admin_approve_comment  /admin/comments/approve/:id    {:module=>"admin", :controller=>"admin/comments", :action=>"approve"} 
    admin_reject_comment  /admin/comments/reject/:id    {:module=>"admin", :controller=>"admin/comments", :action=>"reject"} 

被聲明爲:

namespace "admin" do 

    resources :comments 

    match '/comments/approve/:id' => 'comments#approve', :as => "approve_comment", :module => "admin" 
    match '/comments/reject/:id' => 'comments#reject', :as => "reject_comment", :module => "admin" 
    end 

和這樣的功能測試:

context "a POST to :approve" do 
    setup do 
     comment = Factory(:comment) 
     sign_in Factory(:admin) 
     post :approve, :id => comment.id 
    end 

    should respond_with :success 
end 

但是,當我運行這個時,我得到:

test: a POST to :approve should respond with 200. (Admin::CommentsControllerTest): 
ActionController::RoutingError: No route matches {:action=>"approve", :id=>339, :controller=>"admin/comments"} 

這裏有什麼問題?我犯了什麼愚蠢的錯誤?

+0

你是如何在路線文件中定義這個的?你能不能請張貼 – 2010-06-17 14:45:35

+0

添加路線聲明 – 2010-06-17 14:56:21

回答

2

這些路線看起來像成員路線給我。所以這樣路由

 
    namespace "admin" do 
    resources :comments do 
     member do 
     get :approve 
     get :reject 
     end 
    end 
    end 

這將產生/ admin/comments /:id/approve等路由。據我所知,這是軌道方式。

0

我認爲最好在資源之前進行匹配。因爲它不檢查它是否好。