title: How to run Behat tests as a logged-in user in Laravel?
body: <p>I’m trying to create some tests for a Laravel application using Behat. Many of the tests require that a logged-in user sees specific data. My approach was this:</p>
  
  <ul>
  <li>Log in as a user with a particular ID with <code>Auth::loginUsingId($id)</code></li>
  <li>Go to a particular URL in my app</li>
  <li>Check that the content I’m expecting is there</li>
  </ul>
  
  <p>Now, in my Behat context, although <code>Auth::check()</code> returns <code>true</code>, the filter I have set up doesn’t seem to see this. <code>Auth::check()</code> in that context returns <code>false</code>, and thus attempts to authenticate (via OAuth to the API my app talks to).</p>
  
  <p>How can I go about testing my app as a logged-in user?</p>
  
  <p>My Behat context file, in case it’s of help:</p>
  
  <pre><code>&lt;?php
  
  use Behat\Behat\Context\ClosuredContextInterface;
  use Behat\Behat\Context\TranslatedContextInterface;
  use Behat\Behat\Context\BehatContext;
  use Behat\Behat\Exception\PendingException;
  use Behat\Gherkin\Node\PyStringNode;
  use Behat\Gherkin\Node\TableNode;
  use Behat\MinkExtension\Context\MinkContext;
  
  /**
   * Features context.
   */
  class FeatureContext extends MinkContext
  {
      /**
       * Laravel application instance.
       *
       * @var Illuminate\Foundation\Application
       */
      protected $app;
  
      /**
       * @static
       * @beforeSuite
       */
      public static function bootstrapLaravel()
      {
          $unitTesting = true;
          $testEnvironment = true;
  
          $app = require_once __DIR__ . '/../../../../bootstrap/start.php';
          $app-&gt;boot();
      }
  
      /**
       * Initializes context.
       * Every scenario gets its own context object.
       *
       * @param array $parameters context parameters (set them up through behat.yml)
       */
      public function __construct(array $parameters)
      {
      }
  
      /**
       * @Given /^I am logged in as user ID (\d+)$/
       */
      public function iAmLoggedInAsUserId($id)
      {
          Auth::loginUsingId($id);
      }
  }
  </code></pre>
  
  <p>And a sample test feature:</p>
  
  <pre><code>Feature: Sample Feature
  
    Scenario: View groups a member is associated with
      Given I am logged in as user ID 49
      And I am on "/group"
      Then I should see "Lorem Ipsum"
  </code></pre>