jeudi 16 avril 2020

Laravel 7 throw exception in test with withoutExceptionHandling()

I try to test an unauthorised route with Laravel unit tests. Even with $this->withoutExceptionHandling(); the test fails with an exception.

Here is my test:

    /** @test */
    public function a_user_without_token__check_login_fails()
    {
        $this->withoutExceptionHandling();

        $response = $this->get(route('checkLogin'));
        $response->assertStatus(302);
    }

The test results:

✕ a user without token  check login fails

  Tests:  1 failed, 16 passed, 48 pending

   Illuminate\Auth\AuthenticationException

  Unauthenticated.

  at vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:82
    78|      * @throws \Illuminate\Auth\AuthenticationException
    79|      */
    80|     protected function unauthenticated($request, array $guards)
    81|     {
  > 82|         throw new AuthenticationException(
    83|             'Unauthenticated.', $guards, $this->redirectTo($request)
    84|         );
    85|     }
    86|

      +30 vendor frames
  31  tests/Unit/Http/Controllers/Auth/Authentication/LoginTest.php:111
      Illuminate\Foundation\Testing\TestCase::get("http://localhost/api/authentication/checkLogin")

Aucun commentaire:

Enregistrer un commentaire