Cakephp单元测试中的测试不完整,被跳过或存在风险

问题描述

我正在使用CakePHP版本:3.7.7

我已经写了下面的测试用例。

public function testDashboard()
    {
    $this->get( __('user/dashboard') );
    $branches   = $this->viewVariable('branches');
    $this->assertEquals(1,!empty($branches));
}

对于此控制器代码

public function dashboard()
{
    $this->loadModel('Branches');
    $this->loadModel('Zones');

    $branches  = $this->Branches->find()
        ->where( ['Branches.status' => 1] )
        ->contain(['Zones'])
        ->order(['Branches.id' => 'ASC','Branches.name' => 'ASC'])
        ->toArray();// If I put first() then it works

    $this->set(compact('branches'));
}

当我运行测试用例时,我会遇到错误

测试代码或测试代码没有(仅)关闭自己的输出缓冲区 可以,但是测试不完整,已跳过或存在风险! 测试:1,断言:2,冒险:1。

我可以在以下情况下工作

  1. 如果我放first()代替toArray()
  2. 或者只需设置$branches = []

如何解决此问题?

感谢您的时间。

解决方法

例如,在评估模板时触发异常时,可能会发生这种情况,这将导致用于捕获模板输出的输出缓冲区未关闭,从而导致该错误。该问题已分别在CakePHP 3.9.24.1.3中修复,因此您可能要考虑升级。

话虽如此,请检查CakePHP调试/错误日志,您应该在其中找到引发此问题的潜在错误。您是否需要使用first()toArray()取决于该数据需要的代码。

,

在我的情况下,Branches中有100多个条目会导致此错误,因此我将其减少为