Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rfussien opened this issue Sep 25, 2018 · 2 comments
Closed

Comments

@rfussien
Copy link

  • 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.

@devcircus
Copy link
Contributor

I believe this is the cause: #21625

A user was created, so laravel attaches a 201 created by default.
When you perform another query as in your second example, to retrieve the user from the database, it is no longer a newly created model.

@laurencei
Copy link
Contributor

Thanks @devcircus .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants