简体   繁体   English

如何使用“link_to”和“current_page?”测试RSpec a Rails插件

[英]How to test with RSpec a Rails plugin using “link_to” and “current_page?”

I'm writing a Rails plugin that builds up a menu in a view. 我正在编写一个Rails插件,可以在视图中构建菜单。 I'm using link_to to build the link and current_page? 我正在使用link_to来构建链接和current_page? to set class="active" on the current page. 在当前页面上设置class="active"

I've include d ActionView::Helpers::UrlHelper so I can use link_to . 我已经include D ActionView::Helpers::UrlHelper所以我可以使用link_to

To get current_page? 获取current_page? working in the view, I've had to inherit the current class (apparently ActionView::Base ) and it works perfectly well. 在视图中工作,我必须继承当前的类(显然是ActionView::Base )并且它非常有效。 Unfortunately, this totally breaks the tests with RSpec. 不幸的是,这完全打破了RSpec的测试。

I'm calling link_to and current_page? 我正在调用link_tocurrent_page? like this: 像这样:

def menu(options = {}, &block)
  carte = my_menu.new(self)
  yield carte
  carte.to_s unless carte.empty?
end

class my_menu
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper

  def initialize(base)
    @base = base
  end

  def link
    @base.link_to(name, url_options, html_options)
    @base.current_page?(url_options)
  end
end

And I get this error with RSpec: Undefined method `link_to' for #< Spec::Rails::Example::RailsExampleGroup::Subclass_1:0x24afa90> 我用RSpec得到了这个错误:未定义的方法`link_to'用于#<Spec :: Rails :: Example :: RailsExampleGroup :: Subclass_1:0x24afa90>

Any clue? 任何线索?

include ActionView::Helpers::UrlHelper

In your Spec should solve the problem. 在你的规范中应该解决问题。 Or in your spec_helper.rb. 或者在spec_helper.rb中。 I think this is basically what view specs do by default. 我认为这基本上是默认情况下的视图规范。

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

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