title: Can't make assertions because exceptions are crashing my test in Laravel
body: <p>I want to test the response when I try to send invalid data to the API.</p>
  
  <pre><code>/** @test */
  public function request_schedule_with_invalid_bdate()
  {
      $response = $this-&gt;json('GET', '/api/schedule', [
        'bdate' =&gt; 'thisaintadate',
      ]);
  
      $response-&gt;assertStatus(422);
  }
  </code></pre>
  
  <p>According to the <a href="https://laravel.com/docs/5.7/validation" rel="nofollow noreferrer">documentation</a> it should return a 422</p>
  
  <blockquote>
    <p>If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests.</p>
  </blockquote>
  
  <p>Also,</p>
  
  <blockquote>
    <p>When using the validate method during an AJAX request, Laravel ... generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.</p>
  </blockquote>
  
  <p>It sounds like Laravel should automatically handle the exception thrown and proceed with sending the response.</p>
  
  <p>However, running this in PHPUnit would just cause an error.</p>
  
  <pre><code>There was 1 error:
  
  1) Tests\Feature\ScheduleTest::request_schedule_with_invalid_bdate
  Illuminate\Validation\ValidationException: The given data was invalid.
  </code></pre>
  
  <p>I read <a href="https://stackoverflow.com/questions/5683592/phpunit-assert-that-an-exception-was-thrown">this question</a> but using <code>$this-&gt;expectException(...);</code> will make the test pass but not run my assertions. If I assert the status to be something else other than 422, it will still pass.</p>
  
  <p>My controller has this: </p>
  
  <pre><code>public function show(Request $request) 
  {
    $attributes = $request-&gt;validate([
      'bdate' =&gt; 'required|date'
    ]);
  
    return ['data' =&gt; "It's valid."]
  }
  </code></pre>
  
  <p>Here is my ExceptionHandler class (as requested by Martin H.) This is just what's out of the box. I haven't touched this class yet.</p>
  
  <pre><code>&lt;?php
  
  namespace App\Exceptions;
  
  use Exception;
  use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  
  class Handler extends ExceptionHandler
  {
      /**
       * A list of the exception types that are not reported.
       *
       * @var array
       */
      protected $dontReport = [
          //
      ];
  
      /**
       * A list of the inputs that are never flashed for validation exceptions.
       *
       * @var array
       */
      protected $dontFlash = [
          'password',
          'password_confirmation',
      ];
  
      /**
       * Report or log an exception.
       *
       * @param  \Exception  $exception
       * @return void
       */
      public function report(Exception $exception)
      {
          parent::report($exception);
      }
  
      /**
       * Render an exception into an HTTP response.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Exception  $exception
       * @return \Illuminate\Http\Response
       */
      public function render($request, Exception $exception)
      {
          return parent::render($request, $exception);
      }
  }
  
  </code></pre>
  
  <p>I have confirmed the following:</p>
  
  <ul>
  <li>Laravel Framework 5.7.21 (<code>php artisan --version</code> output)</li>
  <li><p>It runs the appropriate code/controller based on the stack trace:</p>
  
  <p>.../vendor/laravel/framework/src/Illuminate/Validation/Validator.php:315
  .../vendor/laravel/framework/src/Illuminate/Validation/Factory.php:136
  .../vendor/laravel/framework/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php:53
  .../vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:108
  .../app/Http/Controllers/MyController.php:35</p></li>
  </ul>
  
  <p>What am I missing?</p>