phpUnit中的createMock和getMockBuilder有什么区别?
What is the difference between createMock and getMockBuilder in phpUnit?
为了我一生的挚爱,我无法弄清楚 createMock($type)
和 getMockBuilder($type)
之间的区别
我正在查看原始文档,但只有一行我不明白。
... you can use the getMockBuilder($type) method to customize the test double generation using
a fluent interface.
如果你能提供一个例子,我将不胜感激。谢谢。
来自手册
https://phpunit.de/manual/current/en/test-doubles.html
The createMock($type) and getMockBuilder($type) methods provided by
PHPUnit can be used in a test to automatically generate an object that
can act as a test double for the specified original type (interface or
class name). This test double object can be used in every context
where an object of the original type is expected or required.
The createMock($type) method immediately returns a test double object
for the specified type (interface or class). The creation of this test
double is performed using best practice defaults (the __construct()
and __clone() methods of the original class are not executed and the
arguments passed to a method of the test double will not be cloned.
If these defaults are not what you need then you can use the
getMockBuilder($type) method to customize the test double generation
using a fluent interface.
关于堆栈溢出什么是流畅的接口,他们已经有了很多答案。
createMock($type) 在内部使用 getMockBuilder()
:
protected function createMock($originalClassName)
{
return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
因此 createMock()
方法将为您 return 使用一般最佳实践默认值构建的模拟。
但是使用 getMockBuilder($type),您可以根据自己的要求创建模拟。
为了我一生的挚爱,我无法弄清楚 createMock($type)
和 getMockBuilder($type)
我正在查看原始文档,但只有一行我不明白。
... you can use the getMockBuilder($type) method to customize the test double generation using a fluent interface.
如果你能提供一个例子,我将不胜感激。谢谢。
来自手册 https://phpunit.de/manual/current/en/test-doubles.html
The createMock($type) and getMockBuilder($type) methods provided by PHPUnit can be used in a test to automatically generate an object that can act as a test double for the specified original type (interface or class name). This test double object can be used in every context where an object of the original type is expected or required.
The createMock($type) method immediately returns a test double object for the specified type (interface or class). The creation of this test double is performed using best practice defaults (the __construct() and __clone() methods of the original class are not executed and the arguments passed to a method of the test double will not be cloned.
If these defaults are not what you need then you can use the getMockBuilder($type) method to customize the test double generation using a fluent interface.
关于堆栈溢出什么是流畅的接口,他们已经有了很多答案。
createMock($type) 在内部使用 getMockBuilder()
:
protected function createMock($originalClassName)
{
return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
因此 createMock()
方法将为您 return 使用一般最佳实践默认值构建的模拟。
但是使用 getMockBuilder($type),您可以根据自己的要求创建模拟。