[英]How to use polymorphic_path in a functional test in Rails 3
I'm trying to use polymorphic_path
in a functional test in Rails 3. 我正在尝试在Rails 3中的功能测试中使用polymorphic_path
。
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: undefined method error
停止,但现在常规路径(例如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'
I used to be able to use polymorphic_path normally in Rails 2 by including 我曾经能够通过包含在Rails 2中正常使用polymorphic_path
include ActionController::UrlWriter
How can I get this to work in Rails 3? 如何在Rails 3中使用它?
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/ 我通过这篇文章发现了一个类似的问题http://steve.dynedge.co.uk/2010/04/29/rails-3-rake-and-url_for/
I encountered this problem when refactoring some Rails4 tests to make them model-agnostic. 我在重构一些Rails4测试时遇到了这个问题,使它们与模型无关。 I found that 我找到
something_path(obj)
worked but 工作但是
polymorphic_path(obj)
didnt. 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
. 这是在self
是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
Have you tried: 你有没有尝试过:
Rails.application.routes.url_helpers.polymorphic_path
? ? :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.