How to call an app helper method from an RSpec test in Rails?

前端 未结 6 2002
太阳男子
太阳男子 2021-02-05 02:56

The title is self explanatory.

Everything I\'ve tried led to a \"undefined method\".

To clarify, I am not trying to test a helper method. I am trying to use a he

6条回答
  •  终归单人心
    2021-02-05 03:22

    Based on Thomas Riboulet's post on Coderwall:

    At the beginning of your spec file add this:

    def helper
      Helper.instance
    end
    
    class Helper
      include Singleton
      include ActionView::Helpers::NumberHelper
    end
    

    and then call a particular helper with helper.name_of_the_helper.

    This particular example includes the ActionView's NumberHelper. I needed the UrlHelper, so I did include ActionView::Helpers::UrlHelper and helper.link_to.

提交回复
热议问题