PHPUnit 模拟对象和方法类型提示
2022-08-30 11:31:48
我正在尝试使用PHPunit创建\SplObserver的模拟对象,并将模拟对象附加到\SplSubject。当我尝试将模拟对象附加到实现 \SplSubject 的类时,我收到一个可捕获的致命错误,指出模拟对象未实现 \SplObserver:
PHP Catchable fatal error: Argument 1 passed to ..\AbstractSubject::attach() must implement interface SplObserver, instance of PHPUnit_Framework_MockObject_Builder_InvocationMocker given, called in ../Decorator/ResultCacheTest.php on line 44 and defined in /users/.../AbstractSubject.php on line 49
或多或少,这是代码:
// Edit: Using the fully qualified name doesn't work either
$observer = $this->getMock('SplObserver', array('update'))
->expects($this->once())
->method('update');
// Attach the mock object to the cache object and listen for the results to be set on cache
$this->_cache->attach($observer);
doSomethingThatSetsCache();
我不确定它是否有区别,但我使用的是PHP 5.3和PHPUnit 3.4.9