【问题标题】:Incomplete, skipped, or risky tests in Cakephp Unit TestingCakephp 单元测试中的不完整、跳过或有风险的测试
【发布时间】:2021-01-03 10:30:06
【问题描述】:

我使用的是 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 版本(lib/Cake/VERSION.txtvendor/cakephp/cakephp/VERSION.txt 中的最后一行) - 谢谢!

标签: php unit-testing phpunit cakephp-3.0


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2021-12-23
      • 2015-11-17
      • 1970-01-01
      • 2013-05-31
      相关资源
      最近更新 更多