title: rspec + Devise: current_user is nil in tests
body: <p>I am using Devise for my user logins and stuff and rspec for testing. I have looked at the Devise testing guide for rspec and mixined ControllerMicros to controller specs.
  And actually things are all working fine if I have tests organized like this:</p>
  
  <pre><code>  describe 'GET #index' do
      context 'user logged in but not admin' do
        login_user
        it 'should redirect to root_path for non_user' do
          get :index
          // I have asserted that the current_user here is not nil
          expect(response).to redirect_to(root_path)
        end
      end
    end
  </code></pre>
  
  <p>However, if I have 2 tests in the context and I got current_user is nil for the non-first test.</p>
  
  <pre><code>  describe 'GET #index' do
      context 'user logged in but not admin' do
        login_user
        it 'should redirect to root_path for non_user' do
          get :index
          // I have asserted that the current_user here is not nil
          expect(response).to redirect_to(root_path)
        end
  
        it 'should do some other thing' do
          get :index
          // the current_user method returns nil here
          expect(response).to redirect_to(root_path)
        end
      end
    end
  </code></pre>
  
  <p>And the worst part is that it seems this problem is not deterministic: happens somewhat randomly--cause after several failed runs the suite just passed on my computer(but still fails on Travis <a href="https://travis-ci.org/nusskylab/nusskylab/builds/92557703" rel="nofollow">my build</a>)</p>
  
  <p>Some additional information:</p>
  
  <p>the ControllerMacro.rb</p>
  
  <pre><code>module ControllerMacros
    def login_admin
      before(:each) do
        # @request.env["devise.mapping"] = Devise.mappings[:user]
        user = User.find_by(email: 'default_admin@controller.spec')
        user ||= FactoryGirl.create(:user, email: 'default_admin@controller.spec', uid: 'default_admin.controller.spec')
        admin = Admin.find_by(user_id: user.id)
        FactoryGirl.create(:admin, user: user) if not admin
        sign_in user
      end
    end
  
    def login_user(user = nil)
      before(:each) do
        # @request.env["devise.mapping"] = Devise.mappings[:user]
        user ||= User.find_by(email: 'default_user@controller.spec')
        user ||= FactoryGirl.create(:user, email: 'default_user@controller.spec', uid: 'default_user.controller.spec')
        sign_in user
      end
    end
  end
  </code></pre>
  
  <p>the rails_helper.rb</p>
  
  <pre><code>RSpec.configure do |config|
    # for loading devise in test
    config.include Devise::TestHelpers, :type =&gt; :controller
    config.extend ControllerMacros, :type =&gt; :controller
  end
  </code></pre>