我正在为一个小班进行单元测试。我正在使用这个类来使用 PHPUnit,这样我就可以开始正确地测试我将来编写的更大的代码片段。
考虑我正在尝试测试的以下代码:
/**
* Registers a starting benchmark tick.
*
* Registers a tick with the ticks registry representing the start of a benchmark timeframe.
*
* @param string $id The identifier to assign to the starting tick. Ending tick must be the same.
* @return bool Returns TRUE if a tick was registered successfully or FALSE if it was not.
* @since 0.1
*/
public function start($id)
{
$this->tick($id . "_start");
if($this->getStart($id) != false) {
return true;
}
return false;
}
/**
* Retrieves a registered start tick.
*
* Checks to see if a start tick is registered. If found the microtime value (as a float) is
* returned, otherwise FALSE is returned.
*
* @param string $id The identifier to lookup the tick under.
* @return mixed The microtime (as a float) assigned to the specified tick or FALSE if the tick
* start hasn't been registered.
* @since 0.1
*/
public function getStart($id)
{
if(isset($this->ticks[$id . "_start"])) {
return $this->ticks[$id . "_start"];
}
return false;
}
以下是实际的测试代码:
public function testBadStartTick()
{
$this->assertFalse($this->bm->start("What_Invalid_Key_Fits_Here?"))
}
问题是这个测试函数总是返回,true
无论我试图让它返回多少次false
。我尝试过给出空值、300 多个字符的键、空数组,甚至是新对象的实例。在所有情况下,PHP 要么中断,要么抛出某种警告。当 PHP 没有中断时,我的值将转换为 PHP 将在数组键中接受的值,然后我的测试在尝试执行$this->assertFalse()
.
我想实现尽可能多的代码覆盖率。
所以我的问题是,如果这些方法,给定它们当前的代码,是否会false
在正常操作下返回?
我在想,因为我正在附加文本(这是出于管理目的),所以我总是提供某种 PHP 将接受的密钥,无论我提供什么$id
。
有什么想法吗?
提前致谢!