PHPUnit断言没有调用方法

时间:2021-06-01 20:29:43

I have a ClassA that uses a ServiceB. In a certain case, ClassA should end up not invoking any methods of ServiceB. I now want to test this and verity no methods are indeed called.

我有一个使用ServiceB的ClassA。在某种情况下,ClassA最终不应该调用任何ServiceB方法。我现在想测试这个并且确实没有确实调用任何方法。

This can be done as follows:

这可以按如下方式完成:

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

Is there a way to simply state "no method should be called on this object" rather then having to specify a restriction for each method?

有没有办法简单地说“不应该在这个对象上调用方法”,而不是必须为每个方法指定一个限制?

1 个解决方案

#1


38  

Yes, it's quite simple, try this:

是的,这很简单,试试这个:

$classA->expects($this->never())->method($this->anything());

#1


38  

Yes, it's quite simple, try this:

是的,这很简单,试试这个:

$classA->expects($this->never())->method($this->anything());