【发布时间】:2015-03-14 10:48:05
【问题描述】:
我是 PHPUnit 的新手,需要一些关于如何正确测试 Daemon 类、start() 和 runTask() 方法的建议。
class Daemon {
public function start() {
// Start the loop
while (true) {
// This is the code you want to loop during the service...
$this->runTask();
// If signaled to stop
if ('stop' == $this->sig) {
// Break the loop
break;
}
// Now before we 'Task' again, we'll sleep for a bit...
usleep($this->delay);
}
}
}
我尝试使用模拟,但它似乎不起作用。
$mock = $this->getMock('Daemon');
$mock->expects($this->once())->method('runTask');
$mock->start();
【问题讨论】:
-
我认为在这种情况下,您只需测试
runTask()方法而不测试start()方法。