有没有办法验证所有控制器操作,如config/routes.rb
中定义并由rake routes
公开,实际上对应于现有的控制器操作?
例如,假设我们有以下路由文件:
Application.routes.draw do
resources :foobar
end
以下控制器:
class FoobarsController < ApplicationController
def index
# ...
end
def show
# ...
end
end
我想有一些方法可以自动检测create
,new
,edit
,update
和destroy
动作(由路径隐式定义)未映射到有效的控制器动作 - 这样我就可以修复routes.rb
文件:
Application.routes.draw do
resources :foobar, only: [:index, :show]
end
如果愿意,可以对路线进行“完整性检查”。
这种检查不一定需要完美;我可以轻松地手动验证任何误报。 (虽然“完美”检查是理想的,因为它可以包含在测试套件中!)
我的动机是防止AbstractController::ActionNotFound
异常被狡猾的API请求引发,因为无意中定义了额外的路由(在大型应用程序中)。
我很好奇,以下是我的尝试。它仍然不准确,因为它还不匹配正确的format
。此外,一些路线有限制;我的代码还没有考虑。
rails console
:
todo_skipped_routes = []
valid_routes = []
invalid_routes = []
Rails.application.routes.routes.each do |route|
controller_route_name = route.defaults[:controller]
action_route_name = route.defaults[:action]
if controller_route_name.blank? || action_route_name.blank?
todo_skipped_routes << route
next
end
# TODO: maybe Rails already has a "proper" way / method to constantize this
# copied over @max answer, because I forgot to consider namespacing
controller_class = "#{controller_route_name.sub('\/', '::')}_controller".camelcase.safe_constantize
is_route_valid = !controller_class.nil? && controller_class.instance_methods(false).include?(action_route_name.to_sym)
# TODO: check also if "format" matches / gonna be "responded to" properly by the controller-action
# check also "lambda" constraints, and `request.SOMEMETHOD` constraints (i.e. `subdomain`, `remote_ip`, `host`, ...)
if is_route_valid
valid_routes << route
else
invalid_routes << route
end
end
puts valid_routes
puts invalid_routes
# puts "friendlier" version
pp invalid_routes.map(&:defaults)
# => [
# {:controller=>"sessions", :action=>"somenonexistingaction"},
# {:controller=>"posts", :action=>"criate"},
# {:controller=>"yoosers", :action=>"create"},
# ]
我也有兴趣知道其他答案,或者是否有正确的方法来做到这一点。此外,如果有人知道我的代码有所改进,请告诉我。谢谢 :)
这建立在Jay-Ar Polidario的答案之上:
require 'test_helper'
class RoutesTest < ActionDispatch::IntegrationTest
Rails.application.routes.routes.each do |route|
controller, action = route.defaults.slice(:controller, :action).values
# Some routes may have the controller assigned as a dynamic segment
# We need to skip them since we can't really test them in this way
next if controller.nil?
# Skip the built in Rails 5 active_storage routes
next if 'active_storage' == controller.split('/').first
# Naive attempt to resolve the controller constant from the name
# Replacing / with :: is for namespaces
ctrl_name = "#{controller.sub('\/', '::')}_controller".camelcase
ctrl = ctrl_name.safe_constantize
# tagging SecureRandom.uuid on the end is a hack to ensure that each
# test name is unique
test "#{ctrl_name} controller exists - #{SecureRandom.uuid}" do
assert ctrl, "Controller #{ctrl_name} is not defined for #{route.name}"
end
test "#{controller} has the action #{action} - #{SecureRandom.uuid}" do
assert ctrl.respond_to?(action),
"#{ctrl_name} does not have the action '#{action}' - #{route.name}"
end if ctrl
end
end
但是我会质疑它是否真的可用于除了最微不足道的例子之外的任何东西。