【问题标题】:PHP Unit assert no method is called except somePHP单元断言除了一些方法之外没有方法被调用
【发布时间】:2016-09-27 07:14:11
【问题描述】:

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

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

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

【问题讨论】:

  • 我已经删除了我的答案,因为他们都没有工作,对此感到抱歉。

标签: php unit-testing phpunit


【解决方案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')),
        )
    );

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多