Rails 自定义路线上的功能测试

发布于 2024-09-05 21:53:27 字数 2081 浏览 9 评论 0原文

我的应用程序中有以下路由:

                       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"}

这里出了什么问题?我犯了什么愚蠢的错误?

I have the following routes in my app:

                       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"}

which is declared as:

  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

and a functional test like this:

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

However, when I run this I get:

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

What's wrong here? What stupid mistake am I making?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2024-09-12 21:53:27

这些路线对我来说看起来像是会员路线。因此以这种方式路由

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

这将生成类似 /admin/comments/:id/approve 的路由。据我所知,这是铁路方式。

These routes look like member routes to me. So routing this way

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

This will generate routes like /admin/comments/:id/approve . This is the rails way as far i know.

公布 2024-09-12 21:53:27

我认为最好将匹配放在资源之前。因为它不检查它是否好。

I think it's better to put match before resources. Because it's not check if it's good or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文