如何在 Rails 3 的功能测试中使用 polymorphic_path

发布于 2024-11-29 02:28:17 字数 1095 浏览 2 评论 0原文

我正在尝试在 Rails 3 的功能测试中使用polymorphic_path

起初我会得到

NoMethodError: undefined method `polymorphic_path' for #<ArticlesControllerTest:0x492f17c>

然后我添加了

include Rails.application.routes.url_helpers

未定义的方法错误停止,但现在常规路径,例如article_path(article) 例如停止工作:

NameError: undefined local variable or method `default_url_options' for #<ArticlesControllerTest:0x33ccbe0>
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:102:in `url_options'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:131:in `url_for'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/route_set.rb:195:in `article_path'

我曾经能够在 Rails 2 中正常使用 polymorphic_path,方法是包含

include ActionController::UrlWriter

如何让它在 Rails 3 中工作?

I'm trying to use polymorphic_path in a functional test in Rails 3.

At first I would get

NoMethodError: undefined method `polymorphic_path' for #<ArticlesControllerTest:0x492f17c>

And then I added

include Rails.application.routes.url_helpers

The undefined method error stopped, but now regular paths, like article_path(article) for example stopped working:

NameError: undefined local variable or method `default_url_options' for #<ArticlesControllerTest:0x33ccbe0>
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:102:in `url_options'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:131:in `url_for'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/route_set.rb:195:in `article_path'

I used to be able to use polymorphic_path normally in Rails 2 by including

include ActionController::UrlWriter

How can I get this to work in Rails 3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

初懵 2024-12-06 02:28:17

我需要包括:

include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers

并设置:

default_url_options[:host] = 'www.example.com'

我通过这篇文章发现回答了类似的问题
http://steve.dynedge.co。 uk/2010/04/29/rails-3-rake-and-url_for/

I need to include:

include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers

and set:

default_url_options[:host] = 'www.example.com'

I found out via this post that answers a similar issue
http://steve.dynedge.co.uk/2010/04/29/rails-3-rake-and-url_for/

小梨窩很甜 2024-12-06 02:28:17

你尝试过吗:

Rails.application.routes.url_helpers.polymorphic_path

? :)

Have you tried:

Rails.application.routes.url_helpers.polymorphic_path

? :)

一口甜 2024-12-06 02:28:17

我在重构一些 Rails4 测试以使它们与模型无关时遇到了这个问题。我发现这

something_path(obj)

有效但

polymorphic_path(obj)

没有。上述建议都不适合我。然而,我确实发现确实如此:

@controller.polymorphic_path(obj)

这是在 selfActionController::TestCase 的后代的上下文中。

解决此问题的另一种方法是在控制器测试类中定义委托方法:

def polymorphic_path(*args) 
  @controller.polymorphic_path(*args)
end

I encountered this problem when refactoring some Rails4 tests to make them model-agnostic. I found that

something_path(obj)

worked but

polymorphic_path(obj)

didnt. None of the above suggestions worked for me either. I did find that this did, however:

@controller.polymorphic_path(obj)

This is in the context where self is a descendent of ActionController::TestCase.

Another way to work around this is do define a delegation method in the controller test class:

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