gpt4 book ai didi

PHPUnit 断言一个参数具有多个 stringContains 的模拟方法

转载 作者:行者123 更新时间:2023-12-02 01:05:48 25 4
gpt4 key购买 nike

有没有办法为多个匹配断言模拟类方法字符串参数?

$this->getMock()
->expects($this->any())
->method('setString')
->with($this->stringContains('word3'))
->will($this->returnSelf());

这个例子将传递给 ->setString('word1 word2 word3 word4')

如果调用 setString() 时参数包含 'word1' 和 'word3',我需要做的是匹配

但是

$this->getMock()
->expects($this->any())
->method('setString')
->with(
$this->stringContains('word1'),
$this->stringContains('word3')
)
->will($this->returnSelf());

此实现正在检查 setString() 的 2 个参数,这不是我打算检查的。

想法?使用 $this->callback() ?是否有一些更适合此的 PHPUnit 断言?

最佳答案

我知道这个答案已经很晚了,但我遇到了同样的问题并找到了一个似乎更聪明的解决方案......:)

试试这个:(未针对您的情况进行测试)

$this->getMock()
->expects($this->any())
->method('setString')
->with($this->logicalAnd(
$this->stringContains('word1'),
$this->stringContains('word3')
))
->will($this->returnSelf());

关于PHPUnit 断言一个参数具有多个 stringContains 的模拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22673142/

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