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

前端 未结 6 1993
太阳男子
太阳男子 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:17

    I'm assuming you're trying to test the helper method. In order to do this you'll have to put your spec file into spec/helpers/. Given you're using the rspec-rails gem, this will provide you a helper method that allows you to call any helper method on it.

    There's a nice example over in the official rspec-rails documentation:

    require "spec_helper"
    
    describe ApplicationHelper do
      describe "#page_title" do
        it "returns the default title" do
          expect(helper.page_title).to eq("RSpec is your friend")
        end
      end
    end
    

提交回复
热议问题