Jas*_*els 3 php phpunit mocking
当在模拟对象上调用任何未配置的方法时,是否有可能让 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)
这是通过禁用自动返回值生成来完成的。
$foo = $this->getMockBuilder(Foo::class)
->disableAutoReturnValueGeneration()
->getMock();
$foo->expects($this->any())->method('hello')->with('world');
$foo->hello('world');
$foo->bye();
Run Code Online (Sandbox Code Playgroud)
这将导致
Return value inference disabled and no expectation set up for Foo::bye()
Run Code Online (Sandbox Code Playgroud)
请注意,其他方法(如hello)不需要定义返回方法。
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |