Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I'm using link_to to build the link and current_page? to set class="active" on the current page.

I've included ActionView::Helpers::UrlHelper so I can use link_to.

To get current_page? working in the view, I've had to inherit the current class (apparently ActionView::Base) and it works perfectly well. Unfortunately, this totally breaks the tests with RSpec.

I'm calling link_to and current_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>

Any clue?

like image 592
Christian Lescuyer Avatar asked Mar 02 '23 06:03

Christian Lescuyer


1 Answers

include ActionView::Helpers::UrlHelper

In your Spec should solve the problem. Or in your spec_helper.rb. I think this is basically what view specs do by default.

like image 179
user195678 Avatar answered Mar 05 '23 15:03

user195678