Yii2 Codeception error: Call to a member function on null

That kind of error may appear when running Codeception version 2.4.1 tests in a terminal using Yii2 framework. From a different set of tests on null error may appear in any place with different Yii::$app calls. You may notice that error appears every ~20th test. If you run tests separately one by one all of them will be passed successfully.

Yii2 Codeception 2.4.1 error: Call to a member function on null

The member function may be different:

  • Call to a member function getDb() on null
  • Call to a member function getRequest() on null
  • Call to a member function getUrlManager() on null

But in all cases, the error occurs when Yii::$app is called.

How to fix Call to a member function on null error in Codeception 2.4.1

The version 2.4.1 introcudes this problem. Version 2.4.0 works fine. The problem was described on April 5 at GitHub issues page #4922 of Codeception. To fix it go to your composer.json file. It is located at your project root folder. Don't touch composer.lock file. It will be updated automatically. Tweak the version of codeception/codeception:

        "require-dev": {
        "yiisoft/yii2-debug": "~2.0.0",
        "yiisoft/yii2-gii": "~2.0.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/codeception": "2.4.x-dev",
        "codeception/verify": "~0.3.1"
    },

You may set version to 2.4.0 or 2.4.x-dev to get the latest version.

Update Composer packages

Now we need to update codeception/codeception package. Run terminal, go to your project root folder and execute the command:

composer update --prefer-dist

As a result, codeception/codeception package will be updated to fd45d57 commit.

Update composer package Codeception to the new version

Run Codeception tests again

Now problem should be fixed. Let's run tests on the new version of Codeception. Execute this command in the terminal:

php ./vendor/codeception/codeception/codecept run

Run Codeception tests in the terminal

Codeception tests successfully passed

As you can see the problem was fixed and you can continue with testing using Codeception and Yii framework.


25 may 2018 19 july 2018 724


More like this


Comments