测试' skip_after_action' application_controller中的方法

时间:2016-01-04 10:52:41

标签: ruby-on-rails ruby rspec

我的应用程序控制器中有this

skip_after_action :intercom_rails_auto_include, if: :current_staff_user?

private 

def current_staff_user?
  session[:staff_user_id]
end

我想知道如何用rspec正确测试这个方法。我查了the help

我发现的唯一方法就是这个:

  describe 'disable intercom when user is impersonate' do
    controller do
      skip_after_action :intercom_rails_auto_include, if: :current_staff_user?

      def index
        user_is_admin?
      end
    end

    context 'when user is not impersonate' do
      subject { get(:index, {'bim': 50}) }

      before { expect(controller).to receive(:current_staff_user?).twice.and_return(false) }

      it { is_expected.to redirect_to root_path(locale: :en)}
    end

    context 'when user is not impersonate' do
      subject { get(:index, {'bim': 50}, {'staff_user_id': 50}) }

      before { expect(controller).to receive(:current_staff_user?).and_return(true) }

      it { is_expected.to redirect_to root_path(locale: :en) }
    end
  end

如果不在我的测试中重写skip_after_action :intercom_rails_auto_include, if: :current_staff_user?,有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您正在尝试测试应用程序控制器。我相信您需要使用匿名控制器并将过滤器放在那里,就像您正在使用此代码块一样

controller do
  skip_after_action :intercom_rails_auto_include, if: :current_staff_user?
  ...
end

我不喜欢它。 真的。就像在测试中编写代码一样...来测试您是否正确编写了test code。但是......我相信使用AnonymousController似乎确实是该做什么的共识。也许有一天会有人想出更好的方式。

答案 1 :(得分:0)

扭转期望,明确表示已被跳过。否则,您的解决方案将尽可能接近您的解决方案。

expect(controller).to not_receive(:intercom_rails_auto_include)
相关问题