3
2

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のテストを行う

Posted at

表題の通り、Rspec内でaction名に左右されるメソッドをテストする場合、下記の様にmockオブジェクトでaction_nameの返却値を設定してやることで実施できます。

sample_helper.rb
module SampleHelper
  def get
    action_name
  end
end
sample_helper_spec.rb
require 'spec_helper'

describe SampleHelper do
  describe '#get' do
    context 'アクション=showの場合' do
      before do
        allow(helper).to receive(:action_name).and_return('show')
      end
      it 'show が返却される' do
        actual = helper.get
        expected = 'show'
        expect(actual).to eq(expected)
      end
    end
  end
end
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?