我不确定是我做错了什么还是PHPUnit和模拟对象的一个bug。基本上,我正在尝试测试当触发$Model->start()
时是否调用了$Model->doSomething()
。
我在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.
code
$Model = $this->getMock('Model',array('doSomething','doNothing')); #$Model->expects($this->once())->method('start'); # 这个方法有效 - undefined