jgravois's avatar

Simplest Test ($this->assertAuthenticatedAs($user);) Failing???

Ok, I feel like I read the entire Google last night ... arrggg!

I cannot get this test to pass nor can I see why. If I comment out $this->assertAuthenticatedAs($user);, it passes.

/** @test */
    public function user_can_login_with_proper_credentials (): void
    {
        $this->withExceptionHandling();

        $user = User::factory()->create([
            'email' => '[email protected]',
            'password' => 'secret',
        ]);

        $this->post('/login', [
            'email' => '[email protected]',
            'password' => 'secret',
        ])->assertRedirect('/');

        $this->assertAuthenticatedAs($user);
    } // end test
The current user is not authenticated.
Failed asserting that null is not null.
 /Users/jongravois/code/eight/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php:89
 /Users/jongravois/code/eight/tests/Feature/Auth/AuthenticationTest.php:57
0 likes
2 replies
martinbean's avatar
Level 80

@jgravois You need to hash the password when creating the user and saving it to the database:

$user = User::factory()->create([
    'email' => '[email protected]',
    'password' => Hash::make('secret'),
]);

$this->post('/login', [
    'email' => '[email protected]',
    'password' => 'secret',
])->assertRedirect('/');

$this->assertAuthenticatedAs($user);
jgravois's avatar

LOLOL ... where were you 1,234,840 Google links ago???

THANKS!!!!!!!!!!!!!

Please or to participate in this conversation.