【问题标题】:Rails Functional test on a custom route自定义路由上的 Rails 功能测试
【发布时间】:2010-06-17 14:35:35
【问题描述】:

我的应用中有以下路线:

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

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

【问题讨论】:

  • 你是如何在路由文件中定义的?你也可以发一下吗
  • 添加了路由声明

标签: ruby-on-rails ruby testing functional-testing


【解决方案1】:

这些路线在我看来就像会员路线。所以这样路由

命名空间“管理员”做 资源:cmets 做 会员做 得到:批准 得到:拒绝 结尾 结尾 结尾

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2011-01-17
      • 2013-09-30
      • 2021-05-14
      相关资源
      最近更新 更多