Rails 4 个路由,不包括 :new 动作中断 rspec 路由测试
Rails 4 routing excluding :new action breaks rspec routing tests
我有一个简单的控制器,它应该只响应显示和索引操作。为了收紧我的路线,我添加了仅处理 :show 和 :index 的条件。问题是这似乎强制显示操作响应 pro_infos/new,其中它可能将 'new' 视为 :id 参数。
这本身不是真正的问题,但它破坏了我的 rspec 新路由测试无法路由。
来自路线:
resources :masters, only: [:show, :index, :new, :create] do
resources :pro_infos, only: [:show, :index]
end
从rspec路由
it "routes to #new" do
expect(:get => "masters/2/pro_infos/new").not_to be_routable
end
rake routes |grep pro_infos
returns:
master_pro_infos GET /masters/:master_id/pro_infos(.:format) pro_infos#index
master_pro_info GET /masters/:master_id/pro_infos/:id(.:format) pro_infos#show
我知道我可以删除新操作的路由测试,但为了在我的应用程序中保持一致,我更愿意测试不响应它的路由。正确/最佳实践方法是什么?
您可以向您的路由添加一个约束(参见 here)以仅允许像这样的整数 ID:
resources :masters, only: [:show, :index, :new, :create] do
resources :pro_infos, only: [:show, :index], constraints: { id: /\d+/ }
end
我有一个简单的控制器,它应该只响应显示和索引操作。为了收紧我的路线,我添加了仅处理 :show 和 :index 的条件。问题是这似乎强制显示操作响应 pro_infos/new,其中它可能将 'new' 视为 :id 参数。
这本身不是真正的问题,但它破坏了我的 rspec 新路由测试无法路由。
来自路线:
resources :masters, only: [:show, :index, :new, :create] do
resources :pro_infos, only: [:show, :index]
end
从rspec路由
it "routes to #new" do
expect(:get => "masters/2/pro_infos/new").not_to be_routable
end
rake routes |grep pro_infos
returns:
master_pro_infos GET /masters/:master_id/pro_infos(.:format) pro_infos#index
master_pro_info GET /masters/:master_id/pro_infos/:id(.:format) pro_infos#show
我知道我可以删除新操作的路由测试,但为了在我的应用程序中保持一致,我更愿意测试不响应它的路由。正确/最佳实践方法是什么?
您可以向您的路由添加一个约束(参见 here)以仅允许像这样的整数 ID:
resources :masters, only: [:show, :index, :new, :create] do
resources :pro_infos, only: [:show, :index], constraints: { id: /\d+/ }
end