我正在进行一个小单元测试,其中我软删除了一行。为了标记测试成功,我必须用
找到那一行- 给定ID和
-
deleted_at
列不应为空。
我可以满足第一个条件-因为显然我知道ID。
不幸的是,我不知道如何告诉seeInDatabase
方法,我希望deleted_at
不是 null:
$this->seeInDatabase(
'diary_note_categories',
[
'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc',
'deleted_at' => null // should be is not null, like <> or != or whatever
]
);
提示吗?
'deleted_at <>' => null
break
'deleted_at' => ['!=' => null]
也有断点
我这样做了:
$this->seeInDatabase('diary_note...',['id' => 'a7e35ad0'])
->notSeeInDatabase('diary_note...',['id' => 'a7e35ad0','deleted_at'=>null]);
我检查了两步
- 我检查是否有一个记录与我们的id在表
- 我检查是否没有记录与我们的id和deleted_at = null在表
目前还不可能。seeInDatabase
和notSeeInDatabase
都只是将数组直接传递给查询生成器的where
方法,当传递数组时,它不理解如何处理=
以外的任何东西。
https://github.com/laravel/framework/blob/2b4b3e3084d3c467f8dfaf7ce5a6dc466068b47d/src/Illuminate/Database/Query/Builder.php L452
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
// If the column is an array, we will assume it is an array of key-value pairs
// and can add them each as a where clause. We will maintain the boolean we
// received when the method was called and pass it into the nested where.
if (is_array($column)) {
return $this->whereNested(function ($query) use ($column) {
foreach ($column as $key => $value) {
$query->where($key, '=', $value);
}
}, $boolean);
}
// ...
}
选项1 -将以下代码添加到您的TestCase类中,您可以从
扩展测试用例要点:https://gist.github.com/EspadaV8/73c9b311eee96b8e8a03
<?php
/**
* Assert that a given where condition does not matches a soft deleted record
*
* @param string $table
* @param array $data
* @param string $connection
* @return $this
*/
protected function seeIsNotSoftDeletedInDatabase($table, array $data, $connection = null)
{
$database = $this->app->make('db');
$connection = $connection ?: $database->getDefaultConnection();
$count = $database->connection($connection)
->table($table)
->where($data)
->whereNull('deleted_at')
->count();
$this->assertGreaterThan(0, $count, sprintf(
'Found unexpected records in database table [%s] that matched attributes [%s].', $table, json_encode($data)
));
return $this;
}
/**
* Assert that a given where condition matches a soft deleted record
*
* @param string $table
* @param array $data
* @param string $connection
* @return $this
*/
protected function seeIsSoftDeletedInDatabase($table, array $data, $connection = null)
{
$database = $this->app->make('db');
$connection = $connection ?: $database->getDefaultConnection();
$count = $database->connection($connection)
->table($table)
->where($data)
->whereNotNull('deleted_at')
->count();
$this->assertGreaterThan(0, $count, sprintf(
'Found unexpected records in database table [%s] that matched attributes [%s].', $table, json_encode($data)
));
return $this;
}
选项2 -安装以下编译器包
这个composer包与上面的代码完全相同,但是是为composer打包的。
composer require kirkbater/soft-deletes
然后在特定的测试类中使用它:
<?php
use Kirkbater'Testing'SoftDeletes;
class MyTestClass extends TestClass {
use SoftDeletes;
}
这是一个老问题,但是对于那些使用最新版本的Laravel(5.4及以上)的人来说,现在有一个assertSoftDeleted
断言:
所以原来问题的答案现在是:
$this->assertSoftDeleted('diary_note_categories', [
'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc'
]);
断言给定的记录已被删除(Laravel 5.4及以上)。
assertSoftDeleted(string|Model $table, array $data = [], string|null $connection = null)
id:
的示例$this->assertSoftDeleted('table_name', ['id'='value'])
model:
示例$user = User::factory()->create();
$user->delete();
$this->assertSoftDeleted($user);
我在Laravel 6中使用过
$this->assertDatabaseMissing('stores', [
'id' => $test_data['store']->id, 'deleted_at' => null
]);
$this->assertDatabaseHas('stores', ['id' => $id]);
没有测试,但试着这样做:
$this->seeInDatabase(
'diary_note_categories',
[
'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc',
'deleted_at' => ['deleted_at' ,'!=', null ] // should be is not null, like <> or != or whatever
]
);