我有一个使用ServiceB的ClassA.在某种情况下,ClassA最终不应该调用任何ServiceB方法.我现在想测试这个并且确实没有确实调用任何方法.
这可以按如下方式完成:
$classA->expects( $this->never() )->method( 'first_method' );
$classA->expects( $this->never() )->method( 'second_method' );
...
Run Code Online (Sandbox Code Playgroud)
有没有办法简单地说"不应该在这个对象上调用方法"而不是必须为每个方法指定一个限制?
当在模拟对象上调用任何未配置的方法时,是否有可能让 PHPUnit 失败?
例子;
$foo = $this->createMock(Foo::class);
$foo->expects($this->any())->method('hello')->with('world');
$foo->hello('world');
$foo->bye();
Run Code Online (Sandbox Code Playgroud)
这个测试会成功。我希望它失败
Foo::bye() was not expected to be called.
Run Code Online (Sandbox Code Playgroud)
PS 以下可以工作,但这意味着我必须在回调中列出所有配置的方法。这不是一个合适的解决方案。
$foo->expects($this->never())
->method($this->callback(fn($method) => $method !== 'hello'));
Run Code Online (Sandbox Code Playgroud)