title: Unable to stub helper method with rspec
body: <p>I am trying to stub a method on a helper that is defined in my controller. For example:</p>
  
  <pre><code>class ApplicationController &lt; 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
  </code></pre>
  
  <p>In my Rspec:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>In <code>spec/support/authentication.rb</code></p>
  
  <pre><code>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 =&gt; :controller
    config.include RspecAuthentication, :type =&gt; :view
    config.include RspecAuthentication, :type =&gt; :helper
  end
  </code></pre>
  
  <p>I asked a similar question <a href="https://stackoverflow.com/questions/7998491/stub-a-controller-helper-method-in-a-helper-spec">here</a>, but settled on a work around. This strange behavior has creeped up again and I would like to understand <em>why</em> this doesnt work.</p>
  
  <p><em>UPDATE</em>: I have found that calling <code>controller.stub!(:current_user).and_return(@user)</code> before <code>helper.stub!(...)</code> is what is causing this behavior. This is easy enough to fix in <code>spec/support/authentication.rb</code>, but is this a bug in Rspec? I dont see why it would be expected to not be able to stub a method on a helper if it was already stubbed on a controller.</p>