Невозможно заглушить вспомогательный метод с помощью rspec

Я пытаюсь заглушить метод помощника, который определен в моем контроллере. Например:

class ApplicationController < ActionController::Base
  def current_user
    @current_user ||= authenticated_user_method
  end
  helper_method :current_user
end

module SomeHelper
  def do_something
    current_user.call_a_method
  end
end

В моем Rspec:

describe SomeHelper
  it "why cant i stub a helper method?!" do
    helper.stub!(:current_user).and_return(@user)
    helper.respond_to?(:current_user).should be_true # Fails
    helper.do_something # Fails 'no method current_user'
  end
end

В spec/support/authentication.rb

module RspecAuthentication
  def sign_in(user)
    controller.stub!(:current_user).and_return(user)
    controller.stub!(:authenticate!).and_return(true)

    helper.stub(:current_user).and_return(user) if respond_to?(:helper)
  end
end

RSpec.configure do |config|
  config.include RspecAuthentication, :type => :controller
  config.include RspecAuthentication, :type => :view
  config.include RspecAuthentication, :type => :helper
end

Я задал аналогичный вопрос здесь, но остановился на обходном пути. Это странное поведение снова появилось, и я хотел бы понять , почемуэто не работает.

ОБНОВЛЕНИЕ: я обнаружил, что вызов controller.stub!(:current_user).and_return(@user)перед helper.stub!(...)является что вызывает такое поведение. Это достаточно легко исправить в spec/support/authentication.rb, но является ли это ошибкой в ​​Rspec? Я не понимаю, почему можно было бы ожидать, что метод не сможет заглушить помощник, если он уже заглушен на контроллере.

22
задан Community 23 May 2017 в 12:16
поделиться