Skip to content

$request->user() in test is not returning the expected status in a test #25775

@rfussien

Description

@rfussien
  • Laravel Version: 5.7.6
  • PHP Version: 7.2
  • Database Driver & Version: any

Description:

With a basic installation of laravel and passport, when I run a basic test of the default route, it fails with a different response status than the one expected.

Steps To Reproduce:

  • Fresh install of laravel + passport
    public function testBasicTest()
    {
        Passport::actingAs(
            factory(User::class)->create()
        );

        $response = $this->get('api/user');

        $response->assertStatus(200);
    }
$ ./vendor/bin/phpunit
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 703 ms, Memory: 22.00MB

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 201.
Failed asserting that false is true.

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

If I replace the $request->user() by something else, it works.

Route::middleware('auth:api')->get('/user', function (Request $request, User $user) {
	return $user->find($request->user()->id);
	// return $request->user();
});
$ ./vendor/bin/phpunit
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 328 ms, Memory: 22.00MB

OK (2 tests, 2 assertions)

It behaves that way in the test and correctly when call with a http client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions