The testing helpers assertSoftDeleted() and assertNotSoftDeleted() both accept either a table name provided as a string, or a model that implements the SoftDeletes trait. When a model is provided these functions automatically filter the query to the table for the model, as well as looking for the record with the provided primary key.
However, when a model is provided any values provided in the second argument, $data, are discarded when the function recursively calls itself. As a result, what appears to be a valid test case is actually not executed with the expected behavior:
// Assume a database table (example_table) has the following record:
id | col_a | col_b | deleted_at
1 | foo | null | 2021-11-16 00:00:00
// Assume we have a "$model" variable that exists for this record.
// Now, in a test we add the following assertion:
$this->assertSoftDeleted($model, ['col_b' => 'bar']);
The example above will pass, even though the database does not contain a record for that model where col_b is equal to bar. However, if you give that assertion the following code, it does fail as expected:
Description:
The testing helpers
assertSoftDeleted()
andassertNotSoftDeleted()
both accept either a table name provided as a string, or a model that implements theSoftDeletes
trait. When a model is provided these functions automatically filter the query to the table for the model, as well as looking for the record with the provided primary key.However, when a model is provided any values provided in the second argument,
$data
, are discarded when the function recursively calls itself. As a result, what appears to be a valid test case is actually not executed with the expected behavior:The example above will pass, even though the database does not contain a record for that model where
col_b
is equal tobar
. However, if you give that assertion the following code, it does fail as expected:The code responsible for these functions is here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php#L90-L132
The specific chunk responsible for dropping the
$data
parameter is here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php#L123-L125