ltiefland's avatar

Delete account test fails

I'm experimenting with Laravel. Now I've got a problem. I have activated SoftDeletes in the User-Model. Whenever I run the tests I get ONE failed test and it is the "test_user_accounts_can_be_deleted" method.

I get the following response:

Failed asserting that App\Models\User Object #2494 (
    'connection' => 'mysql',
    'table' => 'users',
    'primaryKey' => 'id',
    'keyType' => 'int',
    'incrementing' => true,
    'with' => Array &0 [],
    'withCount' => Array &1 [],
    'preventsLazyLoading' => false,
    'perPage' => 15,
    'exists' => true,
    'wasRecentlyCreated' => false,
    'escapeWhenCastingToString' => false,
    'attributes' => Array &2 [
        'id' => 4,
        'name' => 'Mr. Shaun Rempel II',
        'email' => '[email protected]',
        'email_verified_at' => '2024-01-20 19:52:53',
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
        'two_factor_secret' => null,
        'two_factor_recovery_codes' => null,
        'two_factor_confirmed_at' => null,
        'remember_token' => 'GEMPVprS7b8zg20eUNIEnC9HoK3BACIldEKSV9Ek8smmzGB8sDXBArj74lgC',
        'current_team_id' => null,
        'profile_photo_path' => null,
        'deleted_at' => '2024-01-20 19:52:53',
        'created_at' => '2024-01-20 19:52:53',
        'updated_at' => '2024-01-20 19:52:53',
        'isAdmin' => 0,
    ],
    'original' => Array &3 [
        'id' => 4,
        'name' => 'Mr. Shaun Rempel II',
        'email' => '[email protected]',
        'email_verified_at' => '2024-01-20 19:52:53',
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
        'two_factor_secret' => null,
        'two_factor_recovery_codes' => null,
        'two_factor_confirmed_at' => null,
        'remember_token' => 'GEMPVprS7b8zg20eUNIEnC9HoK3BACIldEKSV9Ek8smmzGB8sDXBArj74lgC',
        'current_team_id' => null,
        'profile_photo_path' => null,
        'deleted_at' => '2024-01-20 19:52:53',
        'created_at' => '2024-01-20 19:52:53',
        'updated_at' => '2024-01-20 19:52:53',
        'isAdmin' => 0,
    ],
    'changes' => Array &4 [],
    'casts' => Array &5 [
        'email_verified_at' => 'datetime',
        'deleted_at' => 'datetime',
        'isAdmin' => 'boolean',
    ],
    'classCastCache' => Array &6 [],
    'attributeCastCache' => Array &7 [],
    'dateFormat' => null,
    'appends' => Array &8 [
        0 => 'profile_photo_url',
    ],
    'dispatchesEvents' => Array &9 [],
    'observables' => Array &10 [],
    'relations' => Array &11 [],
    'touches' => Array &12 [],
    'timestamps' => true,
    'usesUniqueIds' => false,
    'hidden' => Array &13 [
        0 => 'password',
        1 => 'remember_token',
        2 => 'two_factor_recovery_codes',
        3 => 'two_factor_secret',
    ],
    'visible' => Array &14 [],
    'fillable' => Array &15 [
        0 => 'name',
        1 => 'email',
        2 => 'password',
        3 => 'isAdmin',
    ],
    'guarded' => Array &16 [
        0 => '*',
    ],
    'rememberTokenName' => 'remember_token',
    'accessToken' => null,
    'forceDeleting' => false,
) is null.

  at tests\Feature\DeleteAccountTest.php:27
     23▕         $response = $this->delete('/user', [
     24▕             'password' => 'password',
     25▕         ]);
     26▕
  ➜  27▕         $this->assertNull($user->fresh());
     28▕     }
     29▕
     30▕     public function test_correct_password_must_be_provided_before_account_can_be_deleted(): void
     31▕     {

  1   tests\Feature\DeleteAccountTest.php:27

However: When I try it in the app everything works totally fine. I'm using PHPStorm on Windows 11 as editor. What's going on here? BTW: It does NOT matter which database I use (mysql or sqlinte).

I use Jetstream + Inertia + Vue

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like the issue you're encountering is related to the assertion in your test. When using SoftDeletes in Laravel, the fresh() method will return a non-null model if the model has been soft-deleted (i.e., the deleted_at column is set). This is because the model still exists in the database, just marked as deleted.

Instead of asserting that the model is null, you should assert that the deleted_at attribute is not null. This will confirm that the model has been soft-deleted.

Here's how you can adjust your test:

public function test_user_accounts_can_be_deleted(): void
{
    // Assuming you have a user to delete
    $user = User::factory()->create();

    // Perform the delete action here
    // ...

    // Refresh the model
    $user->refresh();

    // Assert the user has been soft-deleted
    $this->assertNotNull($user->deleted_at);
}

This test will now pass if the deleted_at column is set, which indicates a successful soft delete operation. Remember to replace the comment // Perform the delete action here with the actual code that performs the deletion, such as a form request to the route that handles the deletion in your application.

carloeusebi's avatar

I run in the same issue, did you manage to resolve it?

Please or to participate in this conversation.