【发布时间】:2018-03-14 01:26:39
【问题描述】:
假设我有这个非常基础的课程:
class MyClass {
private $_counters = [
'counter1' => 0,
];
public function plus($counter) {
$this->_counters[$counter]++;
}
}
现在对代码进行了更改,以便在 $counter 不存在时它不会中断:
public function plus($counter) {
if (array_key_exists($counter, $this->_counters)) {
$this->_counters[$counter]++;
}
}
我将如何测试这个可能导致抛出“未定义索引”错误的回归?
只需使用不存在的$counter 调用函数plus() 就足够了吗?当一切都好时,什么都不会发生,但如果回归删除了 array_key_exists() 检查,例如,“未定义索引”将被抛出。
我对单元测试很陌生,所以我错过了一种更好的方法来测试这种情况吗?在没有断言的情况下编写测试用例感觉有点奇怪。
【问题讨论】: