Search code examples
laravelunit-testinglaravel-5phpunitlaravel-5.3

Laravel & PHPUnit: Getting 500 when unit testing "Passport" restricted route


I have a Laravel 5.3 app.

Inside my api.php file, there's a route for posting an answer within a poll.

Route::group(['middleware' => 'auth:api'], function () { Route::post('/poll/answer', 'API\PollsController@createAnswer'); });

The route is part of a group, restricted by the auth:api middleware, using Laravel's Passport engine.

When calling this route from Postman or any other tool to test APIs, I get 401 which is ok because I'm not attaching the token.

But when unit testing this call using PHPUnit, it returns 500. I have no idea why.

$this->postJson('api/poll/answer');

I'm probably missing a configuration or setup instruction.

Any ideas?


Solution

  • The 500 error was occurring because I forgot to add an app key to the .env.testing file.

    It got solved after adding that.