我想在一个扩展Threaded
的类中模拟一个方法class Tclass extends \Threaded
{
public function __construct( $name )
{
$this->name = $name;
}
public function run()
{
$this->mockMePlease("var");
}
public function mockMePlease($var){}
}
class TclassTest extends \Codeception\Test\Unit
{
public function testMe()
{
$mongoDist = Stub::make(
'\foo\Tclass',
array(
'mockMePlease' => function($var) {
})
);
}
}
执行我的单元测试我有:
[TypeError]参数1传递给 PHPUnit_Framework_MockObject_InvocationMocker :: __ construct()必须是 类型数组,给定对象,调用 ... / PHPUnit的/ PHPUnit的-模拟对象/ SRC /框架/ MockObject / Generator.php(345) :第54行的eval()代码
有什么想法可以避免这种情况吗?
Codeception version 2.2.9
PHP 7.0.7 (cli) (built: Sep 20 2016 12:46:45) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with DBG v8.2.4, (C) 2000,2016, by Dmitri Dmitrienko
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
[*] pthreads 3.1.7dev
答案 0 :(得分:1)
它不会起作用。来自docs:
当设置为Threaded 的成员时,数组强制转换为Volatile
使\Threaded
的任何衍生物与PHPUnit模拟不兼容。
框架使用array
类型提示参数InvocationMocker::__construct,它使用模拟方法的私有数组作为参数从模拟类中调用。
由于mock继承自模拟类,因此\Threaded
,所有私有数组实际上都是Volatile
个对象。
简单来说,以下代码失败并出现相同的错误:
class A extends Threaded
{
private $a = [1,2,3];
public function run()
{
return $this->runArray($this->a);
}
public function runArray(array $v)
{
return $v;
}
}
var_dump((new A)->runArray([1,2,3])); // works. array is outside of \Threaded
var_dump((new A)->run()); // TypeError - A::$a is an object.