gpt4 book ai didi

PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any,什么时候用?

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:04 24 4
gpt4 key购买 nike

刚找到PHPUnit_Framework_TestCase::any .

Returns a matcher that matches when the method it is evaluated for is executed zero or more times.

据我所知,方法总是被“零次或多次”调用,我从未调用过方法 -1 次或更少次。所以这个匹配器应该总是匹配的。为什么它存在以及何时使用它?

最佳答案

这是一个小代码示例,显示了方法 ::method()

class SomeClass
{
public function method(){
// smth is being done here;
}
}

class SomeClassTest extends PHPUnit_Framework_TestCase
{
private $mock;

public function setUp(){
$this->mock = $this->getMockBuilder('SomeClass')
->getMock();
}

/**
* @test
*/
public function canStub(){
$this->mock->expects($this->any())
->method('method')
->willReturn(true);
$this->assertTrue($this->mock->method());
}

/**
* @test
*/
public function willCauseFatalError(){
$this->mock->method('method')
->willReturn(true);
$this->assertTrue($this->mock->method());
}
}

第二次测试将给出 fatal error :调用成员函数 willReturn() on null

更重要的是,如果 SomeClass 除了 method 还有任何其他方法,它们也需要通过 ->expects($this->any( ))...

关于PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any,什么时候用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42206398/

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