Codeception:测试基于Yii2的应用程序,带有“原因”;“连接太多”;错误

Codeception:测试基于Yii2的应用程序,带有“原因”;“连接太多”;错误,yii2,codeception,fixtures,Yii2,Codeception,Fixtures,以下示例显示了我如何以任何需要的方法加载和卸载夹具: class ServiceTest extends \Codeception\Test\Unit { protected $tester; protected function loadFixture() { $this->tester->haveFixtures(['user' => ['class' => UserFixture::class]]); }

以下示例显示了我如何以任何需要的方法加载和卸载夹具:

class ServiceTest extends \Codeception\Test\Unit
{

    protected $tester;

    protected function loadFixture()
    {
        $this->tester->haveFixtures(['user' => ['class' => UserFixture::class]]);
    }

    protected function unLoadFixture()
    {
        $this->tester->grabFixture('user')->db->close();
    }

    public function testSuccessSignin()
    {
        $this->loadFixture();
        $form   = new SigninForm([
            'email'    => 'brady.renner@rutherford.com',
            'password' => '123456',
        ]);
        $result = Service::signin($form, new \yii\web\User([
            'identityClass' => Identity::class,
        ]));
        $this->assertTrue($result);
        $this->unLoadFixture();
    }
}
但是当我运行上面这样的测试时,
db->close()
似乎无法正常工作,
SHOW PROCESSLIST
显示了许多处于“睡眠”状态的连接。
当我通过
\u before()
fixture()
方法加载fixture时也会发生同样的情况(这些方法是不必要的原因,并非所有方法都需要该fixture)。

您需要为数据库连接传递
属性
持久连接
true
,请参阅此处的问题

编辑

除了上面给出的解决方案,如果不适合您,您可以在评论中尝试
@Mik
建议,在codeception套装配置中的yii2模块设置下设置
cleanup:false
,请参见

,这种方法在我的情况下不起作用,但我发现了一个好提示:在codeception Yii模块配置中添加字符串
cleanup:false
。现在可以正常工作了@Mik我很感激你能接受你很高兴你找到了修复方法
'db' => array(
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_test',
      'username' => 'root',
      'password' => '',
      'tablePrefix' => '',
      'charset' => 'utf8',
      'attributes'=>[
           PDO::ATTR_PERSISTENT => true
      ]
 ),