當在模擬對象上調用任何未配置的方法時,PHPUnit 是否可能失敗?例子;$foo = $this->createMock(Foo::class);$foo->expects($this->any())->method('hello')->with('world');$foo->hello('world');$foo->bye();這個測試會成功。我希望它失敗Foo::bye() was not expected to be called. PS以下將起作用,但這意味著我必須在回調中列出所有配置的方法。這不是一個合適的解決方案。$foo->expects($this->never()) ->method($this->callback(fn($method) => $method !== 'hello'));
1 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
這是通過禁用自動返回值生成來完成的。
$foo = $this->getMockBuilder(Foo::class)
->disableAutoReturnValueGeneration()
->getMock();
$foo->expects($this->any())->method('hello')->with('world');
$foo->hello('world');
$foo->bye();
這將導致
Return value inference disabled and no expectation set up for Foo::bye()
請注意,其他方法(如 )不需要hello定義返回方法。
- 1 回答
- 0 關注
- 116 瀏覽