dimanche 7 mars 2021

laravel 8.x test api returning wrong status code

I have written a basic api which I am trying to write test for but the test is returning 404 instead of 422. How can i fix this so the validation is returning 422?

https://github.com/shorif2000/api

in my auth controller

public function register(Request $request)
    {
        die();
        $validatedData = $request->validate([
            'givenName' => 'required|max:55',
            'familyName' => 'required|max:55',
            'email' => 'email|required|unique:users',
            'password' => 'required|confirmed'
        ]);

        $validatedData['password'] = Hash::make($request->password);

        $user = User::create($validatedData);

        $accessToken = $user->createToken('authToken')->accessToken;

        return response(['user' => $user, 'access_token' => $accessToken, "token_type" => "Bearer"], 201);
    }

but the test does not even get there

php artisan test tests/Feature --filter testRequiredFieldsForRegistration

   FAIL  Tests\Feature\AuthTest
  ⨯ required fields for registration

  ---

  • Tests\Feature\AuthTest > required fields for registration
  Expected status code 422 but received 404.
  Failed asserting that 422 is identical to 404.

  at tests/Feature/AuthTest.php:14
     10▕
     11▕     public function testRequiredFieldsForRegistration()
     12▕     {
     13▕         $this->json('POST', 'api.register', ['Accept' => 'application/json'])
  ➜  14▕             ->assertStatus(422)
     15▕             ->assertJson([
     16▕                 "message" => "The given data was invalid.",
     17▕                 "errors" => [
     18▕                     "givenName" => ["The givenName field is required."],

Aucun commentaire:

Enregistrer un commentaire