我不确定我做错了什么,或者它是 PHPUnit 和模拟对象的错误。基本上我正在尝试测试是否在触发$Model->doSomething()
时被调用。$Model->start()
我在 VirtualBox 中使用 Ubuntu,并通过 pear 安装 phpunit 1.1.1。
完整代码如下。任何帮助将不胜感激,这让我发疯。
<?php
require_once 'PHPUnit/Autoload.php';
class Model
{
function doSomething( ) {
echo 'Hello World';
}
function doNothing( ) { }
function start( ) {
$this->doNothing();
$this->doSomething();
}
}
class ModelTest extends PHPUnit_Framework_TestCase
{
function testDoSomething( )
{
$Model = $this->getMock('Model');
$Model->expects($this->once())->method('start'); # This works
$Model->expects($this->once())->method('doSomething'); # This does not work
$Model->start();
}
}
?>
PHPUnit 的输出:
There was 1 failure:
1) ModelTest::testDoSomething
Expectation failed for method name is equal to <string:doSomething> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.