在 rspec 测试中调用 Rails.application.routes.recognize_path 与 Rails 3 中的任何路由都不匹配
我正在开发的 Rails 3 应用程序包含一些调用以下代码的逻辑(我无法更改):
Rails.application.routes.recognize_path("/customers", :method => :get)
“/customers”当然是变量。
我正在编写一个关联的 Rspec 测试,它调用包含所述逻辑的代码,并且该测试具有完整的 Rails 环境。当我提出以下内容时:
Rails.application.routes.routes.inspect
它包含正确的路由(例如,它具有的路由之一是“GET /customers”)。
然后,当我运行测试时,逻辑结果如下:
No route matches "/customers"
执行以下操作:
@routes = Rails.application.routes
assert_recognizes({:controller => "customers", :action => "index"}, "/customers")
会导致相同的错误。
在辅助测试中,以下内容:
# this succeeds and returns "/customers"
x = helper.customers_path
Rails.application.routes.recognize_path(x, :method => :get)
再次导致相同的错误(没有路由匹配“/customers”)
我 100% 肯定 Rails.application.routes 包含正确的路由。
有人知道这是什么原因吗?
谢谢!
A Rails 3 application I'm working on contains some logic that invokes the following code (which I cannot change):
Rails.application.routes.recognize_path("/customers", :method => :get)
The "/customers" is of course variable.
I'm writing an associated Rspec test, which invokes the code that contains said logic and the test has a complete Rails environment. When I raise the following:
Rails.application.routes.routes.inspect
it contains the proper routes (e.g. one of the routes it has is "GET /customers").
When I then run the test, the logic results in a:
No route matches "/customers"
Doing the following:
@routes = Rails.application.routes
assert_recognizes({:controller => "customers", :action => "index"}, "/customers")
results in the same error.
Within a helper test, the following:
# this succeeds and returns "/customers"
x = helper.customers_path
Rails.application.routes.recognize_path(x, :method => :get)
results in, once again, the same error (No route matches "/customers")
I'm 100% positively sure that Rails.application.routes contain the proper routes.
Does anybody have any idea what the cause of this is?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后发现了这个愚蠢的自我引发的错误的原因:忘记定义路由映射到的 CustomersController。
在深入研究 Rails 的源代码后发现,路由实际上恒定化了映射到路由的控制器,因此您的规范中需要映射到的实际控制器。 :)
Finally came across the cause of this stupid self-induced bug: forgot to define a CustomersController that the routes map to.
After diving into Rails' source found out that routing actually constantizes the controller mapped to the routes, so an actual controller to map to is required in your specs. :)