PHP单元断言没有方法被调用,除了一些

时间:2016-05-29 10:56:36

标签: php unit-testing phpunit

与此类似的问题:PHPUnit assert no method is called

如何断言除了我可以定义的一些方法之外没有调用任何方法?以下测试没有通过,因为PHPUnit验证所有expected()。

//Here assert that only 'firstMethodToBeCalled' and 'secondMethodToBeCalled' are called, and no more
$mock = $this->getMockBuilder('SomeClass')->getMock();
$mock->expects($this->never())
    ->method($this->anything());
$mock->expects($this->once())
    ->method('firstMethodToBeCalled');
$mock->expects($this->once())
    ->method('secondMethodToBeCalled');

1 个答案:

答案 0 :(得分:1)

尝试使用它:

$mock = $this->getMockBuilder('SomeClass')->getMock();

$mock->expects($this->once())
    ->method('firstMethodToBeCalled');
$mock->expects($this->once())
    ->method('secondMethodToBeCalled');
$mock->expects($this->never())
    ->method(
        $this->logicalAnd(
            $this->logicalNot($this->equalTo('firstMethodToBeCalled')),
            $this->logicalNot($this->equalTo('secondMethodToBeCalled')),
        )
    );