【发布时间】:2019-03-22 00:57:48
【问题描述】:
我有这个守卫:
protected function validateRemove($key)
{
if (!isset($this->collection[$key])) {
throw new CategoryCollectionBadItemException();
}
}
还有测试:
/**
* @test
* @expectedException InfluenceDecision\Domain\Exception\Category\CategoryCollectionBadItemException
*/
public function removeMethodMustThrowExceptionWithInvalidKey()
{
$this->categoryCollection->add(
new Category(
null,
'test category'
)
);
$this->categoryCollection->remove(1);
}
CategoryCollection remove 方法调用 validateRemove 方法
测试工作正常,但覆盖率不是 100%,因为测试无法访问 validateRemove 方法的最后一行:
正确的解决方案是什么?
【问题讨论】:
-
覆盖率报告告诉你方法的右大括号未被覆盖?也许您需要一个提供有效集合密钥的测试。不过,我个人认为不值得在这方面投入时间,您已经涵盖了您想要涵盖的逻辑,而且 100% 的代码覆盖率基本上只是一个不错的徽章
标签: php phpunit code-coverage guard-clause