No se puede aplastar el método de ayuda con rspec
Estoy tratando de detener un método en un ayudante que está definido en mi controlador. Por ejemplo:
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
En mi 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
Enspec/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
Hice una pregunta similaraquí, pero se estableció en un trabajo alrededor. Este extraño comportamiento ha vuelto a aparecer y me gustaría entenderlo.por qué esto no funciona
ACTUALIZAR: He encontrado ese llamadocontroller.stub!(:current_user).and_return(@user)
antes dehelper.stub!(...)
Es lo que está causando este comportamiento. Esto es bastante fácil de arreglarspec/support/authentication.rb
, pero es este un error en Rspec? No veo por qué se esperaría que no se pudiera detectar un método en un ayudante si ya estaba en un controlador.