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

deb*_*deb 4 ruby ruby-on-rails functional-testing ruby-on-rails-3 rails-3-upgrade

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

起初我会得到

NoMethodError: undefined method `polymorphic_path' for #<ArticlesControllerTest:0x492f17c>
Run Code Online (Sandbox Code Playgroud)

然后我补充道

include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

我曾经能够通过包含在Rails 2中正常使用polymorphic_path

include ActionController::UrlWriter
Run Code Online (Sandbox Code Playgroud)

如何在Rails 3中使用它?

deb*_*deb 5

我需要包括:

include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)

并设置:

default_url_options[:host] = 'www.example.com'
Run Code Online (Sandbox Code Playgroud)

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