如何在 Rails 3 的功能测试中使用 polymorphic_path
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我需要包括:
并设置:
我通过这篇文章发现回答了类似的问题
http://steve.dynedge.co。 uk/2010/04/29/rails-3-rake-and-url_for/
I need to include:
and set:
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/
你尝试过吗:
? :)
Have you tried:
? :)
我在重构一些 Rails4 测试以使它们与模型无关时遇到了这个问题。我发现这
有效但
没有。上述建议都不适合我。然而,我确实发现确实如此:
这是在
self
是ActionController::TestCase
的后代的上下文中。解决此问题的另一种方法是在控制器测试类中定义委托方法:
I encountered this problem when refactoring some Rails4 tests to make them model-agnostic. I found that
worked but
didnt. None of the above suggestions worked for me either. I did find that this did, however:
This is in the context where
self
is a descendent ofActionController::TestCase
.Another way to work around this is do define a delegation method in the controller test class: