gpt4 book ai didi

php - 如何在 PHPUnit 中获取确定的调用方法的数量

转载 作者:行者123 更新时间:2023-11-28 21:23:53 26 4
gpt4 key购买 nike

目前我正在使用字符串来指定我的测试失败的地方,如下所示:

In the first call to 'XY' method, the first parameter:

所以,想用phpunit获取通话次数。

简而言之,我想要一个基数而不是 first, second, third ...,但是给了 phpunit(更好)

public function testExample()
{
$test = $this;

$this->myClass
->expects($this->exactly(2))
->method('methodOne')
->withConsecutive(
[
$this->callback(function ($arg) use ($test) {
$part = 'In the first call to methodOne method, the first parameter: ';

$test->assertThat(
$arg,
$this->logicalAnd($this->equalTo('example1')),
$part . 'is not equal to "example1" '
);

return true;
}),
],
[
$this->callback(function ($arg) use ($test) {
$part = 'In the first call to methodOne method, the first parameter: ';

$test->assertThat(
$arg,
$this->logicalAnd($this->equalTo('example2')),
$part . 'is not equal to "example2"'
);

return true;
}),
]
)
->will($this->returnSelf());
}

最佳答案

使用预言:

 class A {
function abc($a, $b) {
return ...;
}
}

$a = $this->prophesize (A::class);
$a->abc (1,2)->willReturn ("something");
$A = $a->reveal ();

$A->abc (1, 2);
$A->abc (1, 2);
$A->abc (1, 2);

这为您提供了通话次数:

    $calls = $a->findProphecyMethodCalls ("abc", new ArgumentsWildcard([new AnyValuesToken]));
var_dump (count($calls));

您可以遍历所有调用以查看它们的参数:

    foreach ($calls as $call)
{
var_dump ($call->getArguments());
}

关于php - 如何在 PHPUnit 中获取确定的调用方法的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45848314/

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