PHPUnit 断言方法不等于


PHPUnit assert method not equal to

我有一个使用 ServiceB 的 ClassA。在某些情况下,类 A 最终应该不会调用除一个方法之外的任何 ServiceB 方法。我现在想测试一下,并验证没有其他方法确实被调用。

这可以按如下方式完成:

$classA->expects( $this->once() )->method( 'first_method' );
$classA->expects( $this->never() )->method( 'second_method' );
$classA->expects( $this->never() )->method( 'third_method' );
...

有没有办法简单地声明"不应在此对象上调用任何非首先的方法",而不必为每个方法指定限制?

目前有这种方法有效,尽管使我的测试用例依赖于两个有点内部的 PHPUnit 类,我宁愿避免这样做。

$schema->expects( $this->never() )
            ->method( new PHPUnit_Framework_Constraint_Not( new PHPUnit_Framework_Constraint_IsEqual( 'addField' ) ) );

有没有办法使用流畅的界面做同样的事情?

是的,有:)试试这个:

$classA->expects($this->once())->method('first_method');
$classA->expects($this->never())
    ->method($this->logicalNot($this->equalTo('first_method')));