Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created October 31, 2024 11:26
Show Gist options
  • Save fractaledmind/410e519ccd51445cc10c3408b5f24d77 to your computer and use it in GitHub Desktop.
Save fractaledmind/410e519ccd51445cc10c3408b5f24d77 to your computer and use it in GitHub Desktop.
Test that ensures you have no controllers with public methods that do not have a corresponding route defined.
class RoutingTest < ActionDispatch::IntegrationTest
IGNORED_CONTROLLERS = Set[
"Rails::MailersController"
]
test "no unrouted actions (public controller methods)" do
actions_by_controller.each do |controller_path, actions|
controller_name = "#{controller_path.camelize}Controller"
next if IGNORED_CONTROLLERS.include?(controller_name)
controller = Object.const_get(controller_name)
public_methods = controller.public_instance_methods(_include_super = false).map(&:to_s)
unrouted_actions = public_methods - actions
assert_empty(
unrouted_actions,
"#{controller_name} has unrouted actions (public methods). These should probably be private"
)
end
end
private
def actions_by_controller
{}.tap do |controllers|
@routes.routes.each do |route|
controller = route.requirements[:controller]
action = route.requirements[:action]
next unless controller && action
(controllers[controller] ||= []) << action
end
end
end
end
@hmaddocks
Copy link

There is a gem that does this. It all so does the inverse, checks for defined routes that don’t have corresponding methods.

https://github.com/amatsuda/traceroute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment