How to use polyorphic_path in a functional test in Rails 3

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

I would get first

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

And then I added

 include Rails.application.routes.url_helpers 

undefined method error stopped, but now normal paths, such as article_path(article) , for example, have 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 use a polymorphic path in Rails 2, including

 include ActionController::UrlWriter 

How can I get this to work in Rails 3?

+4
source share
3 answers

I need to include:

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

and install:

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

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

+5
source

I ran into this problem when refactoring some Rails4 tests to make them agnostic models. I found that

 something_path(obj) 

worked but

 polymorphic_path(obj) 

technique fails him. None of the above suggestions helped me. However, I found that this happened:

 @controller.polymorphic_path(obj) 

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

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

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

You tried:

 Rails.application.routes.url_helpers.polymorphic_path 

? :)

-1
source

All Articles