CodeceptionUtilStub::construct('SomeClass')和new So


What is the difference between CodeceptionUtilStub::construct('SomeClass') and new SomeClass?

有两种方法可以在Codeception中创建新的类编写单元测试。

use Codeception'Util'Stub as Stub;
$SomeClass = new SomeClass();
$SomeClass = Stub::construct('SomeClass');

有人能解释一下这两种方法的区别吗?

new SomeClass创建一个类的常规实例,
Stub::construct('SomeClass')创建类的test double,替换一些方法或属性。

见http://codeception.com/docs/reference/Stub#construct

属性和方法可以在第三个参数中设置。甚至可以设置受保护的和私有的属性。

如果您不重写任何属性或方法,则两者之间没有区别(也没有理由使用Stub)。