LoginSignup
4
3

More than 5 years have passed since last update.

Controller に書いた helper メソッドを view spec で stub する

Last updated at Posted at 2019-02-05
# ApplicationController

def user_logged_in?
 'hoge'
end

helper_method :user_logged_in?
# header.html.slim_spec.rb

RSpec.describe 'application/_header', type: :view do
  let(:user) { create(:user) }

  subject do
    render partial: 'application/header'
  end
  before { stub_helper_method }

  def stub_helper_method
    controller.singleton_class.class_exec do
      helper_method :user_logged_in?
      define_method :user_logged_in? do
        'stub'
      end
    end
  end

  context 'not signed in' do
    before do
      allow(view).to receive(:user_logged_in?).and_return(false)
    end

    it do
      is_expected.to have_link 'ログイン', href: new_user_session_path
    end
  end

純粋に以下のみで試みると
allow(view).to receive(:user_logged_in?).and_return(false)

=> does not implement: user_logged_in?

と返されるため。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3