如何在Rails 3中的function测试中使用polymorphic_path
我正在尝试在Rails 3中的function测试中使用polymorphic_path
。
起初我会得到
NoMethodError: undefined method `polymorphic_path' for #
然后我补充道
include Rails.application.routes.url_helpers
undefined method error
停止,但现在常规路径(例如article_path(article)
已停止工作:
NameError: undefined local variable or method `default_url_options' for # .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中使用它?
我需要包括:
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/
我在重构一些Rails4测试时遇到了这个问题,使它们与模型无关。 我找到
something_path(obj)
工作但是
polymorphic_path(obj)
didnt。 以上建议都没有对我有用。 但我确实发现这确实如此:
@controller.polymorphic_path(obj)
这是在self
是ActionController::TestCase
的后代的上下文中。
解决此问题的另一种方法是在控制器测试类中定义委托方法:
def polymorphic_path(*args) @controller.polymorphic_path(*args) end
你有没有尝试过:
Rails.application.routes.url_helpers.polymorphic_path
? 🙂