9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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については下記のサイトに詳しく記載してあります。
http://morizyun.github.io/blog/rspec-model-controller-ruby-rails/

9
9
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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?