gpt4 book ai didi

PHPUnit:期望方法含义

转载 作者:IT王子 更新时间:2023-10-29 00:55:42 25 4
gpt4 key购买 nike

当我创建一个新的模拟时,我需要调用 expects 方法。它到底是做什么的?它的论点呢?

$todoListMock = $this->getMock('\Model\Todo_List');
$todoListMock->expects($this->any())
->method('getItems')
->will($this->returnValue(array($itemMock)));

我在任何地方都找不到原因(我已经尝试过文档)。我已经阅读了来源,但我无法理解。

最佳答案

expects() - Sets how many times you expect a method to be called:

$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'));

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

参见:
PHPUnit mock with multiple expects() calls

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs

http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing

关于PHPUnit:期望方法含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7432458/

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