gpt4 book ai didi

mocking - 具有多个 Expects() 调用的 PHPUnit 模拟

转载 作者:行者123 更新时间:2023-12-02 17:02:07 24 4
gpt4 key购买 nike

使用 PHPUnit,我想知道我们如何从同一个 stub /模拟中获得多种期望。

例如,我想测试模拟是否会调用 display() 方法并返回 NULL。我还想测试 process() 方法是否会被调用。

事实上,我的测试称为 testProcessIsCalledIfDisplayReturnNull()

所以我需要在同一个模拟对象上设置 2 个期望,而手册对此并没有真正的帮助:(

最佳答案

如果你知道,该方法会被调用一次,在expects()中使用$this->once(),否则使用$this->any()

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod'));
$mock->expects($this->once())
->method('firstMethod')
->will($this->returnValue('value'));
$mock->expects($this->once())
->method('secondMethod')
->will($this->returnValue('value'));
$mock->expects($this->once())
->method('thirdMethod')
->will($this->returnValue('value'));

关于mocking - 具有多个 Expects() 调用的 PHPUnit 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834416/

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