Laravel Testcase在将测试配置为使用sqlite时创建重复条目并删除mysql数据库条目
我正在尝试使用TestCase为我的测试添加数据种子 TestCase.phpLaravel Testcase在将测试配置为使用sqlite时创建重复条目并删除mysql数据库条目,laravel,phpunit,Laravel,Phpunit,我正在尝试使用TestCase为我的测试添加数据种子 TestCase.php abstract class TestCase extends BaseTestCase { use CreatesApplication; protected $roleOwner; protected function setUp(): void { parent::setUp(); $this->roleOwner = factory(
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $roleOwner;
protected function setUp(): void
{
parent::setUp();
$this->roleOwner = factory('App\Role')->create();
$this->roleCounsellor = factory('App\Role')->create([
'name' => 'counsellor',
'display_name' => 'Counsellor',
'description' => 'Counsellor on team.'
]);
}
protected function tearDown(): void
{
parent::tearDown();
}
}
$factory->define(Role::class, function (Faker $faker) {
return [
'name' => 'owner',
'display_name' => 'Owner of Team',
'description' => 'Owner of users team.'
];
});
RoleFactory.php
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $roleOwner;
protected function setUp(): void
{
parent::setUp();
$this->roleOwner = factory('App\Role')->create();
$this->roleCounsellor = factory('App\Role')->create([
'name' => 'counsellor',
'display_name' => 'Counsellor',
'description' => 'Counsellor on team.'
]);
}
protected function tearDown(): void
{
parent::tearDown();
}
}
$factory->define(Role::class, function (Faker $faker) {
return [
'name' => 'owner',
'display_name' => 'Owner of Team',
'description' => 'Owner of users team.'
];
});
当我运行所有测试vendor/bin/phpunit
时,数十个测试都会出现此错误:
角色(名称,显示名称,说明,更新位置
,创建位置
)值(所有者,团队所有者,用户团队所有者,2020-07-07 19:59:432020-07-07 19:59:43)
此外,当我运行所有测试时,它会删除mysql数据库中的所有内容,并且我已将测试配置为在sqlite上运行。我认为这是相关的,但我不确定。您必须确保每次测试后都会重置数据库。我认为
刷新数据库
特性就是您要寻找的。请参阅
如果在单元测试中未使用内存中的数据库,则上述特征可能会运行很长时间。因此,您可以使用以下特征
illumb\Foundation\Testing\DatabaseTransactions
。它将运行得更快,因为它不必为每个测试重置数据库。它将每个测试包装在数据库事务中,然后重新启动这样,就不会向数据库写入任何实际更改。您必须确保每次测试后都会重置数据库。我认为刷新数据库的特性就是您要寻找的。请参阅
如果在单元测试中未使用内存中的数据库,则上述特征可能会运行很长时间。因此,您可以使用以下特征illumb\Foundation\Testing\DatabaseTransactions
。它将运行得更快,因为它不必为每个测试重置数据库。它将每个测试包装在数据库事务中,然后重新启动这样,就不会将实际更改写入数据库