Php &引用;没有活动的交易”;在Laravel 8.0测试中刷新数据库时
使用PHP8.0.2和Laravel8.37.0,我正在运行测试,每个测试都应该刷新数据库数据,因为每个测试都有冲突的数据(由于唯一的约束)。Php &引用;没有活动的交易”;在Laravel 8.0测试中刷新数据库时,php,mysql,laravel,phpunit,Php,Mysql,Laravel,Phpunit,使用PHP8.0.2和Laravel8.37.0,我正在运行测试,每个测试都应该刷新数据库数据,因为每个测试都有冲突的数据(由于唯一的约束)。 将内存中数据库与SQLite一起使用,这是可行的,但当我切换到MySQL(v8.0.23)时,我会遇到下一个错误: 1) Tests\Feature\Controllers\AuthControllerTest::testSuccessLogin PDOException: There is no active transaction 由于数据已插入且
将内存中数据库与SQLite一起使用,这是可行的,但当我切换到MySQL(v8.0.23)时,我会遇到下一个错误:
1) Tests\Feature\Controllers\AuthControllerTest::testSuccessLogin
PDOException: There is no active transaction
由于数据已插入且测试后未清除,因此此测试之后的测试失败
我要做的测试是:
<?php
namespace Tests\Feature\Controllers;
use App\Models\User;
use App\Models\User\Company;
use App\Repositories\UserRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthControllerTest extends TestCase
{
use RefreshDatabase;
protected array $connectionsToTransact = ['mysql'];
public function testSuccessLogin(): void
{
$this->artisan('migrate-data');
/** @var User $user */
$user = User::factory()->create([
'email' => 'test@website.com'
]);
$this->app->bind(UserRepository::class, function() use ($user) {
return new UserRepository($user, new Company());
});
$loginResponse = $this->post('/api/login', [
'email' => 'test@website.com',
'password' => 'password'
]);
$loginResponse->assertStatus(200);
$loginResponse->assertJsonStructure([
'data' => [
'user' => [
'name',
'surname',
'email',
'abilities'
],
'token',
]
]);
}
}
这是一个已知的问题吗?还是我遗漏了什么?试着删除
$connectionsToTransact
谢谢你的回答,miken32。我以前试过,但仍然不起作用。在检查源代码时,它会为该数组中的所有连接设置一个事务,因此这有点奇怪。在使用RefreshDatabase
的测试中完成的所有操作都是在事务中完成的,因此不必这样做。错误消息意味着有人试图关闭一个已经关闭的事务,所以我猜这里有冲突。
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<report>
<html outputDirectory="reports/coverage"/>
</report>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="mysql"/>
<server name="DB_HOST" value="localhost"/>
<server name="DB_DATABASE" value="mysql_test"/>
<server name="DB_USERNAME" value="root"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>