Create a controller instance: Call to a member function getModule() on a non-object

I want to test actions of the controller in the Unit test, as described here: Testing controller

For that I need to create the controller instance:

$controller = new SomeController('SomeController', Yii::$app->getModule('somemodule'));

As a result of executing this code I get the error:

Fatal error: Call to a member function getModule() on a non-object in C:\projects\ais\tests\codeception\backend\unit\SomeControllerTest.php on line 27

How to create a controller instance in the Unit test (and in general)?

Please note that the link you provided is Yii 1, not Yii 2. The syntax can be different in Yii 2.

Your $app is not being initialized in your testing environment.

You can view an example about how initialize it in yii2-app-advanced > tests > codeception > frontend > unit > SignupForTest (<< see this). There you will see that each unit test is prepared to ‘bootstrap’ the $app through yii2-codeception > Testcase (<< and see this too).

Hope it help.

Thank you all for your answers!