作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
刚找到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/
刚找到PHPUnit_Framework_TestCase::any . Returns a matcher that matches when the method it is evaluated
我是一名优秀的程序员,十分优秀!