assert not soft deleted? Inverse? - Laravel Tests

Published 1 week ago by krystian2160

Laravels test suite has an assertion called

$this->assertSoftDeleted() But is there the inverse? To assert that something is not soft deleted?

I've tried assertDatabaseHas but doesn't work.

I have done it this way $this->assertTrue(! $goal->fresh()->trashed());

Works perfect.

But is there another, better, more ---elegant--- and laravel's way?

Best Answer (As Selected By krystian2160)
tykus

There is no inverse of assertSoftDeleted; in my opinion your alternative is fine except I would assertFalse($goal->fresh()->trashed())

If you really prefer, you could useassertDatabaseHas as follows:

$this->assertDatabaseHas('table_name', [
    'id' => $goal->id,
    $goal->getDeletedAtColumn() => null
]); 

... but does it express your intention better than the original?

tykus
tykus
1 week ago (527,810 XP)

There is no inverse of assertSoftDeleted; in my opinion your alternative is fine except I would assertFalse($goal->fresh()->trashed())

If you really prefer, you could useassertDatabaseHas as follows:

$this->assertDatabaseHas('table_name', [
    'id' => $goal->id,
    $goal->getDeletedAtColumn() => null
]); 

... but does it express your intention better than the original?

krystian2160

Haha, I agree. Totally forgot about asserFalse XD haha. Yea :D assertTrue with "!" stupid :D

Please sign in or create an account to participate in this conversation.