title: How to add Auth as a controller dependency for testing in Laravel 4
body: <p>I'm starting out with some TDD in Laravel 4. While I understand the basics of dependency injection, I can't seem to get my head around how to mock the Auth functionality. Below is my current user controller with just the index method and it's applicable test. The current setup keeps throwing me errors when I run phpunit, namely "undefined index" errors for Auth. Is there a better way of doing this?</p>
  
  <p><strong>My Controller:</strong></p>
  
  <p>
  
  <pre><code>class UserController extends \BaseController {
  
      /**
       * User instance
       * 
       * @var User
       */
      protected $user;
  
      /**
       * The master layout that View's will be built upon.
       * 
       * @var string
       */
      protected $layout = 'layout.user-master';
  
      /**
       * Constructor
       * 
       * @param User $user
       * @param View $view
       */
      public function __construct(User $user) 
      {
          $this-&gt;user = $user;
  
          // Filters
          $this-&gt;beforeFilter('csrf', array('on' =&gt; 'post'));
          $this-&gt;beforeFilter('auth', array('except' =&gt; array('create', 'store')));
      }
  
      /**
       * Display a listing of the user.
       *
       * @return Response
       */
      public function index()
      {
          $id = Auth::user()-&gt;id;
  
          $user = $this-&gt;user-&gt;find($id);
  
          $this-&gt;layout-&gt;content = View::make('user.index', compact('user'));
      }
  }
  </code></pre>
  
  <p><strong>User Controller Test</strong></p>
  
  <pre><code>&lt;?php
  
  use \Mockery;
  
  class UserControllerTest extends TestCase {
  
      public function __construct()
      {
          // Mock an Eloquent User Model instance
          $this-&gt;mock = Mockery::mock('Eloquent', 'User');    
      }
  
      public function tearDown()
      {
          Mockery::close();
      }
  
      public function testIndex()
      {
          Auth::shouldReceive('user')-&gt;once()-&gt;andReturn(Mockery::any());
  
          $this-&gt;mock
                  -&gt;shouldReceive('find')
                  -&gt;once()
                  -&gt;with(Mockery::any())
                  -&gt;andReturn('foo');
  
          $this-&gt;app-&gt;instance('User', $this-&gt;mock);
  
          $this-&gt;call('GET', 'user');
  
          $this-&gt;assertViewHas('user');
      }
  
  }
  </code></pre>