LoginSignup
9

More than 5 years have passed since last update.

RSpec: helperのテスト

Last updated at Posted at 2015-10-23

application_helper.rb


module ApplicationHelper
  def current_user_approval
    if current_user & current_user.approval
      return true
    else
      return false
    end
  end
end

というhelperメソッドがあるとして

expect(helper.current_user_approval).to eq true

とするとundefined method current_userと出てしまいます。
正しくテストするにはstubして実行してあげましょう。

require 'spec_helper'

describe ApplicationHelper do
  context '承認されている' do
    let(:account) { create(:account, approval: true) }
    before {
      logged_in(account) # ログインしていると仮定させるメソッド
      allow(helper).to receive(:current_user).and_return(account)
    }
    it '成功' do
      expect(helper.current_user_approval).to eq true
    end
  end
end

stubについては下記のサイトに詳しく記載してあります。

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
9