Jor*_*own 3 php phpunit unit-testing mocking
我正在编写一个依赖于外部类exceptionManager的单元测试.我希望能够预测这个类上的某些特定函数会返回什么,所以我使用的是模拟对象.代码非常简单:
$mockExceptionManager = $this->getMock('exceptionManager');
Run Code Online (Sandbox Code Playgroud)
问题是,我的异常管理器实现了IteratorAggregate接口,这需要一个如下所示的方法:
public function getIterator()
{
return new ArrayIterator($this->exceptions);
}
Run Code Online (Sandbox Code Playgroud)
当我运行单元测试时,我收到以下错误:
致命错误:无法在/Applications/MAMP/bin/php5.2/lib/php/PEAR/PHPUnit/Framework/MockObject/Generator.php(170)中重新声明Mock_exceptionManager_ae79bad2 :: getIterator():eval()'d代码在线297
我有一种感觉,PHPUnit模拟对象套件也实现了IteratorAggregate接口,并且两者发生了冲突,尽管我不确定.我也尝试使用Iterator接口,但遇到了同样的问题.我怎么能绕过这个?
我在模拟对象上禁用了自动加载功能,解决了这个问题.
$mockExceptionManager = $this->getMockBuilder('exceptionManager')
->disableAutoload()
->getMock();
Run Code Online (Sandbox Code Playgroud)