Roman Vitruk Roman Vitruk - 2 months ago 21
PHP Question

What is the difference between Codeception\Util\Stub::construct('SomeClass') and new SomeClass?

There are two ways to create new class writing unit tests in Codeception.

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


Can someone explain what is the difference between this two approaches?

Answer

new SomeClass creates a regural instance of the class,
Stub::construct('SomeClass') creates a test double of the class with some methods or properties replaced.

As documented in http://codeception.com/docs/reference/Stub#construct

Properties and methods can be set in third argument. Even protected and private properties can be set.

There is no difference between the two (and no reason to use Stub), if you are not overriding any properties or methods.