Roman Vitruk Roman Vitruk - 1 year ago 144
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 Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download