(我确定我已经看到了这个问题的答案,但是在挖掘了几分钟后找不到它。这应该是一个容易回答的问题......)
动机
在控制台中,您可以get
使用路径参数调用该方法,例如:
app.get posts_path(321)
这会调用ActionDispatch::Integration::RequestHelpers#get
,它将“/posts/321”转换为适当的控制器、动作和参数,例如 {:controller => "posts", :action => "show", :id => "321"}。
但是你不能在控制器测试中这样做。要获得相同的效果,您需要:
get :show, :id => "321"
这是因为get
方法是由 定义的ActionController::TestCase#get
,它期望动作和参数已经被解析出来。
我可以在手动填写 :controller、:action 和任何参数的地方编写测试,但这似乎是引入错误的好方法。
问题
是否有一种方法可以将字符串(例如 的调用posts_path(321)
)解析为适当的 :controller、:action 和参数,适合传递给ActionController::TestCase#get
?
这将使编写测试更简单,更不容易出错。
[编辑:我意识到您需要 HTTP 动词 ( get
) 以及路径 ( /posts/321
) 来唯一地映射到:controller 和:action,但问题仍然存在……]