在 rspec 测试中调用 Rails.application.routes.recognize_path 与 Rails 3 中的任何路由都不匹配

发布于 2024-10-11 18:03:51 字数 927 浏览 7 评论 0原文

我正在开发的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-10-18 18:03:52

最后发现了这个愚蠢的自我引发的错误的原因:忘记定义路由映射到的 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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文