Summer Sale! All accounts are 50% off this week.

fahdshaykh's avatar

Soft delete error in testing

I am trying to delete a blog post and make a delete post also but facing a error during testing. I found the deleted_at is missing in blog_posts table but i have added soft delete in migration with new migration class The error is: There was 1 failure:

  1. Tests\Feature\PostTest::test_delete Failed asserting that any soft deleted row in the table [blog_posts] matches the attributes {"title":"New title","content":"Content of the blog post","user_id":1,"updated_at":"2023-07-04T14:12:31.000000Z","created_at":"2023-07-04T14:12:31.000000Z","id":1}.

Found: [ { "id": 1, "title": "New title", "content": "Content of the blog post", "created_at": "2023-07-04 14:12:31", "updated_at": "2023-07-04 14:12:31", "user_id": 1, "deleted_at": "2023-07-04 14:12:31" } ].

my post test class is postTest.php to delete test:

      public function test_delete()
     {
     $user = $this->user();

     $post = $this->createDummyBlogPost($user->id);

      $data = $post->toArray();

    $this->assertDatabaseHas('blog_posts', [
        'title' => $data['title'],
        'content' => $data['content']
    ]);


    $this->actingAs($user)
        ->delete("/posts/{$post->id}")
        ->assertStatus(302)
        ->assertSessionHas('status');

    $this->assertEquals(session('status'), 'Blog post was deleted!');

    $this->assertSoftDeleted('blog_posts', $post->toArray());
}
0 likes
2 replies
ramonrietdijk's avatar
Level 30

Maybe it has to do with the differently formatted updated_at and created_at values.

What if you only check the ID of the post in the assertSoftDeleted method?

$this->assertSoftDeleted('blog_posts', [
    'id' => $post->id,
]);

You should also be able to pass the model as the first parameter, which does exactly the same thing.

$this->assertSoftDeleted($post);
1 like

Please or to participate in this conversation.