title: RSpec: how to stub inherited method current_user (w/o Devise)?
body: <p>I have a controller based on MHartl's <a href="http://www.railstutorial.org/book/_single-page" rel="noreferrer">RoR4 Tutorial</a></p>
  
  <p>And just like MHartl, <strong>I'm not using Devise</strong>, I <a href="http://www.railstutorial.org/book/_single-page#sidebar-roll_your_own" rel="noreferrer">rolled my own authentication system</a></p>
  
  <p>Having trouble with the RSpec for <code>UsersController#Edit</code> since the view has a call to <code>current_user.admin?</code> and the controller calls <code>@path_switch = path_switch</code></p>
  
  <p>I keep getting RSpec errors along the lines of:</p>
  
  <pre><code>1) User Pages Edit 
   Failure/Error: visit edit_user_path(user)
   NoMethodError:
     undefined method `admin?' for nil:NilClass
   # ./app/controllers/users_controller.rb:106:in `path_switch'
   # ./app/controllers/users_controller.rb:53:in `edit'
   # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p><strong>UsersController:</strong></p>
  
  <pre><code>class UsersController &lt; ApplicationController
    ...
    def edit
      @user = User.find(params[:id])
      @path_switch ||= path_switch                        #error
    end
    ...
    def path_switch
      if current_user.admin?                               #error
        users_path
      else
        root_path
      end
    end
  end
  </code></pre>
  
  <hr>
  
  <p>I found this <a href="http://leeourand.com/2014/02/09/stub-that-object/" rel="noreferrer">really helpful article</a> that gives me hope that I'm on the right track, but I can't get it to work. </p>
  
  <p>Here's as far as I've gotten (updated):</p>
  
  <p><strong>user_pages_spec.rb:</strong></p>
  
  <pre><code>require 'spec_helper'
  require 'support/utilities'
  
  describe "User Pages" do
    #include SessionsHelper
  
    let(:user) { FactoryGirl.create(:user) }
    let(:current_user) {user}
  
    subject { page }
  
    describe "Edit" do
      before do
        sign_in(user)
        visit edit_user_path(user) 
      end
  
      it '(check links and content)' do
        should have_button('Submit')
        should have_link('Cancel')
        should have_content(user.fname+"\'s profile")
      end
     ...
    end
  ...
  end
  </code></pre>
  
  <p>But <code>current_user</code> is still coming back <code>nil</code></p>
  
  <p>Any help/guidance is appreciated. Thanks!</p>
  
  <hr>
  
  <p>Adding <code>include SessionsHelper</code> to the top describe block of my <code>user_pages_edit.rb</code> seems to try and use the sign_in(path) from that helper. Creating an <a href="https://stackoverflow.com/questions/21503397/undefined-method-permanent-when-attempting-to-create-a-cookie">issue between RSpec and <code>cookies.permanent</code></a>. So that's a bust.</p>
  
  <p><strong>unfortunately</strong>, this brings me right back to my <code>.admin?</code> error.</p>
  
  <p>There are two calls to <code>current_user.admin?</code> </p>
  
  <p><strong>One is in the controller:</strong></p>
  
  <pre><code>  def path_switch
      if current_user.admin?    #error current_user == nil
        users_path
      else
        root_path
      end
    end
  </code></pre>
  
  <p><strong>One is in the view as ERB:</strong></p>
  
  <pre><code>&lt;% if current_user.admin? %&gt;
    &lt;div class="row  col-xs-6 col-sm-6 col-md-3"&gt;
      &lt;div class="input-group input-selector"&gt;
      ...
  </code></pre>
  
  <p>All I need to do is figure out how to set <code>current_user.admin = true</code> and pass it to the controller (and then hopefully the view) so that the page can load.
  To do that, all I need to do is set <code>current_user = user</code> because <code>user.admin == true</code>.</p>