raajkumarpaneru's avatar

The test is not passing.

I have written a test for password reset with token.

/** @test */
    public function user_can_reset_password_with_password_reset_token()
    {
        $this->withoutExceptionHandling();

        $user = User::factory()->create();
        $user->password = Hash::make('old-password');
        $user->save();

        $response = $this->postJson('/api/password-reset', [
            'token' => '123456',
            'email' => $user->email,
            'password' => 'new-password',
            'password_confirmation' => 'new-password',
        ]);

        $hash = Hash::check('new-password', $user->password);
        $this->assertEquals(true, $hash);
    }

My respective controller code is as follow:

public function resetPassword(Request $request)
    {
        $user = User::where('email', $request->email)->firstOrFail();
        $user->password = Hash::make($request->password);
        $user->save();

        return response()->json([
            'status' => 'success',
            'data' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
            ]
        ]);
    }

When I run the test, I got an error: Failed asserting that false matches expected true.

Please help me out.

0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to refresh the user

        $hash = Hash::check('new-password', $user->fresh()->password);

Please or to participate in this conversation.