无法模拟具有常量默认参数的方法
Unable to mock a method with constant default param
我在测试方法中有以下代码:
$container = $this
->getMockBuilder(ContainerInterface::class)
->getMock();
$container->method('get')
->will($this->returnValueMap([
['a', 'b'],
['c', 'd']
]));
var_dump($container->get('a'));
exit;
我只是想创建一个存根(基于接口)。但是这个returnsNULL
。但是,如果我将方法从 get
更改为 has
它确实有效(returns b
)。
签名的区别如下:
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
public function has($id);
为什么它不起作用,我该如何解决?
PHPUnit 不支持考虑默认参数。
所以你需要改变这个:
$container->method('get')
->will($this->returnValueMap([
['a', 'b'],
['c', 'd']
]));
进入这个:
$container->method('get')
->will($this->returnValueMap([
['a', 1, 'b'],
['c', 1, 'd']
]));
希望对您有所帮助
我在测试方法中有以下代码:
$container = $this
->getMockBuilder(ContainerInterface::class)
->getMock();
$container->method('get')
->will($this->returnValueMap([
['a', 'b'],
['c', 'd']
]));
var_dump($container->get('a'));
exit;
我只是想创建一个存根(基于接口)。但是这个returnsNULL
。但是,如果我将方法从 get
更改为 has
它确实有效(returns b
)。
签名的区别如下:
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
public function has($id);
为什么它不起作用,我该如何解决?
PHPUnit 不支持考虑默认参数。
所以你需要改变这个:
$container->method('get')
->will($this->returnValueMap([
['a', 'b'],
['c', 'd']
]));
进入这个:
$container->method('get')
->will($this->returnValueMap([
['a', 1, 'b'],
['c', 1, 'd']
]));
希望对您有所帮助