【问题标题】:Why is the line throwing an exception not covered为什么没有覆盖抛出异常的行
【发布时间】:2014-12-30 09:41:49
【问题描述】:

我有一段代码显示为未涵盖,即使我有一个特定的测试来触发它。代码如下:

private function processConfiguration($baseFileName, ConfigurationInterface $definition)
{
    $fileName = $this->createEnvironmentSpecificFileName($baseFileName);

    $processor = new Processor();
    $configuration = $this->loader->load($fileName);

    if (is_null($configuration) === true) {
        throw new \UnexpectedValueException('The configuration file can not be empty.');
    }

    return $processor->processConfiguration($definition, $configuration);
}

并且专门为使该函数抛出异常而进行的单元测试:

public function testGetQueueSettingsWithEmptyFile()
{
    $this->setExpectedException('UnexpectedValueException');
    $loader = $this->mockLoaderWith([], null, 0);
    (new Reader($loader))->getQueueSettings();
}

然而,即使这个单元测试通过了,当我使用可能从 PHPUnit 获取报告的 Codeception 获取覆盖率报告时,此行是红色的:

throw new \UnexpectedValueException('The configuration file can not be empty.');

为什么?

【问题讨论】:

    标签: unit-testing phpunit code-coverage xdebug codeception


    【解决方案1】:

    这显然是我的错误。让$this->setExpectedException() 行抑制由下一行引起的异常。未正确设置模拟,因此从未运行过 if 语句。

    当我在测试中修复代码时,有问题的行被正确标记为run。

    【讨论】:

      猜你喜欢
      • 2011-12-12
      • 2021-07-08
      • 1970-01-01
      • 2020-11-04
      • 2010-12-09
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多