PhpUnit测试刷新初始数据库



我正在使用Laravel 8并使用PhpUnit创建一些单元测试。我已经创建了200多个单元测试,一切都很顺利。今天,当我运行命令php artisan test时,我注意到它比平时花费更多,中途我停止了命令。在那之后,我的初始数据库数据被无故删除。请注意,我在一个专门为单元测试创建的单独数据库上运行单元测试。现在我的单元测试正在.env文件中的初始数据库上运行,我不明白为什么。有什么解决方案吗?这是我创建的一个基本单元测试代码的一个小片段,也许你们能发现一个我似乎找不到的小错误。

这是代码:

<?php
namespace TestsUnit;
use AppModelsAccount;
use AppServicesAccountService;
use IlluminateFoundationTestingRefreshDatabase;
use TestsTestCase;
class AccountTest extends TestCase
{
use RefreshDatabase;
/**
* A basic unit test example.
*
* @return void
*/
public function test_accounts_retrieved_from_database()
{
Account::factory()->create();
$accountService = app(AccountService::class);
$accountService->getAll();
$this->assertDatabaseCount('accounts', 4);
}
}

你好,我遇到了这个问题,我不知道是什么原因造成的,但这个命令帮我解决了这个问题。希望它也能帮助你。

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan config:cache --env=testing

最新更新