How to test Rails plugin using RSpec using "link_to" and "current_page?"

I am writing a Rails plugin that creates menus in a view. Am I using link_to to create a link and current_page? to set class="active" on the current page.

I have include d ActionView::Helpers::UrlHelper , so I can use link_to .

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

Am I link_to and current_page? in the following way:

 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 `link_to 'method for # <Spec :: Rails :: Example :: RailsExampleGroup :: Subclass_1: 0x24afa90>

Any clue?

+4
source share
1 answer
 include ActionView::Helpers::UrlHelper 

Your spec should solve the problem. Or in your spec_helper.rb. I think this is basically what the spec specifications look like by default.

+6
source

Source: https://habr.com/ru/post/1277678/


All Articles