简体   繁体   English

使用Rspec在助手中测试没有路径的link_to

[英]Testing link_to without path in helper using Rspec

Using the tip here I have built a method in a helper module like so: 我在这里使用技巧帮助器模块中构建了一个方法,如下所示:

describe ExampleHelper do
  def build_link
    link_to "RSS feed", params.merge(:format => :rss), :class => "feed_link"
  end
end

However, when I test this method I get the following error: 但是,当我测试此方法时,出现以下错误:

 Failure/Error: subject { helper.build_link }
 ActionController::UrlGenerationError:
   No route matches {:action=>"index", :foo=>"bah", :foobah=>["1", "2", "3"], :results_per_page=>"25"}
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionpack-4.2.6/lib/action_dispatch/journey/formatter.rb:46:in `generate'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionpack-4.2.6/lib/action_dispatch/routing/route_set.rb:721:in `generate'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionpack-4.2.6/lib/action_dispatch/routing/route_set.rb:752:in `generate'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionpack-4.2.6/lib/action_dispatch/routing/route_set.rb:799:in `url_for'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionpack-4.2.6/lib/action_dispatch/routing/url_for.rb:156:in `url_for'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionview-4.2.6/lib/action_view/routing_url_for.rb:94:in `url_for'
 # /Users/tombrammar/.rvm/gems/ruby-2.1.10@example/gems/actionview-4.2.6/lib/action_view/helpers/url_helper.rb:181:in `link_to'

It seems that because I'm testing within the helper it can't determine what controller to use and therefore how to build the url. 似乎是因为我正在帮助程序中进行测试,因此无法确定要使用的控制器以及因此如何构建URL。

Can anyone help me understand how to fix this? 谁能帮助我了解如何解决此问题? Ie perhaps I can stub the controller the helper rspec? 即也许我可以将控制器的助手rspec存根?

This is a dirty trick if you are using params like above. 如果您使用上面的参数,这是一个肮脏的把戏。

allow(helper).to receive(:params).and_return(_recall: { controller: 'homes' })

Also here is another way to do it that is a quick way to get the job done. 另外,这是另一种方法,可以快速完成工作。

https://stackoverflow.com/a/22265920/1431800 https://stackoverflow.com/a/22265920/1431800

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM