gpt4 book ai didi

PHPUnit:如何使用多个参数模拟多个方法调用?

转载 作者:IT王子 更新时间:2023-10-28 23:59:07 25 4
gpt4 key购买 nike

我正在为使用 PHPUnit 的方法编写单元测试。我正在测试的方法在同一个对象上调用相同的方法 3 次,但使用不同的参数集。我的问题类似于提出的问题 herehere

其他帖子中提出的问题与只接受一个参数的模拟方法有关。

但是,我的方法需要多个参数,我需要这样的东西:

$mock->expects($this->exactly(3))
->method('MyMockedMethod')
->with(
$this->logicalOr(
$this->equalTo($arg1, $arg2, arg3....argNb),
$this->equalTo($arg1b, $arg2b, arg3b....argNb),
$this->equalTo($arg1c, $arg2c, arg3c....argNc)
)
);

此代码不起作用,因为 equalTo() 仅验证一个参数。给它多个参数会引发异常:

Argument #2 of PHPUnit_Framework_Constraint_IsEqual::__construct() must be a numeric

有没有办法为具有多个参数的方法进行 logicalOr 模拟?

提前致谢。

最佳答案

就我而言,答案非常简单:

$this->expects($this->at(0))
->method('write')
->with(/* first set of params */);

$this->expects($this->at(1))
->method('write')
->with(/* second set of params */);

关键是使用$this->at(n)n是方法的第N次调用。我无法对我尝试过的任何 logicalOr() 变体做任何事情。

关于PHPUnit:如何使用多个参数模拟多个方法调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com