So I want to unit test my API endpoints. I'm using Laravel 5.8 and api authentication is done with Passport I have the following test:
public function guest_can_login()
{
$user = factory(User::class)->create();
$response = $this->json('POST', 'api/login', [
'email' => $user->email,
'password' => 'secret',
]);
$response
->assertStatus(200)
->assertJson([
'success' => true,
]);
}
when i execute the request with Postman it works well, authenticates user and return a token but when i launch the test it fails with ("Expected status code 200 but received 500.")
So i searched a little deeper in the response and i found this:
"message": "Personal access client not found. Please create one."
no clue why this is happening, if anyone has insight on this
EDIT: fixed see my last answer below
just to make it clear for someone who has the same issue, the problem was that the fact i'm using an inmemory database for testing, it resets everytime you run the tests so of course the passport keys are wiped aswell, that's why adding \Artisan::call('passport:install');
at the start of your test fixes this issue.