如何测试Rails自定义路由?解决路由匹配报错问题
Rails自定义路由测试报错的解决方法
问题场景
我定义了如下Rails路由:
Rails.application.routes.draw do get "my_profile", to: "users#show" end
该路由生成的映射为:
my_profile GET /my_profile(.:format) users#show
我想在UsersControllerTest中验证这个自定义路由是否存在且能执行对应动作,编写了如下测试代码:
class UsersControllerTest < ActionController::TestCase def test_my_profile_route get "/my_profile" # 测试逻辑 end end
但运行时出现报错:
ActionController::UrlGenerationError: No route matches {:action=>"/my_profile", :controller=>"users"}
问题原因
在ActionController::TestCase的测试方法里,get方法的第一个参数是控制器的动作名称,不是URL路径。你传入"/my_profile"会被当成动作名去匹配,自然找不到对应的路由规则,所以报错。
正确测试方式
方法一:使用路由辅助方法(推荐)
Rails会自动为自定义路由生成辅助方法my_profile_path(相对路径)和my_profile_url(绝对路径),直接用这个辅助方法作为get的参数即可:
class UsersControllerTest < ActionController::TestCase def test_my_profile_route get my_profile_path # 验证响应正常 assert_response :success # 可选:验证路由确实指向users#show动作 assert_equal "users", controller.controller_name assert_equal "show", controller.action_name end end
方法二:直接验证路由映射
如果想直接测试URL路径和控制器动作的对应关系,可以用assert_routing断言,这种方式更直接验证路由规则本身:
class UsersControllerTest < ActionController::TestCase def test_my_profile_route_mapping # 验证GET请求的/my_profile路径对应users#show assert_routing "/my_profile", { controller: "users", action: "show" } # 反过来验证:路由辅助方法生成的路径是否正确 assert_generates "/my_profile", { controller: "users", action: "show" } end end
更规范的路由测试方式
专门的路由测试建议放在test/routes目录下的测试文件中(比如routes/users_routing_test.rb),使用ActionDispatch::IntegrationTest来编写:
require "test_helper" class UsersRoutingTest < ActionDispatch::IntegrationTest test "my_profile路由指向users#show" do assert_routing "/my_profile", { controller: "users", action: "show" } get my_profile_path assert_response :success end end
内容的提问来源于stack exchange,提问作者fguillen
相关产品推荐
相关产品推荐

