1
votes

I'm new to Codeception, but I'm running into an issue injecting Page Objects. The problem occurs when I add the following construct logic to my page object.

public function __construct(\AcceptanceTester $I) {
    $this->tester = $I;
}

... I got this from Login Page object example here: http://codeception.com/docs/06-ReusingTestCode#PageObjects

The error I'm getting is:

[Codeception\Exception\InjectionException] Failed to inject dependencies in instance of 'MyCest'. Failed to create instance of 'Page\Login'. Failed to create instance of 'AcceptanceTester'. Failed to create instance of 'Codeception\Scenario'. Failed to resolve dependency 'Codeception\TestCase'.

This is how I'm injecting the page in my Cest.

protected function _inject(\Page\Login $login) {
  $this->login_page = $login;
}

If I remove the __construct code, the error goes away. Is this a bug in Codeception or am I doing something wrong?

This is the work-around I found...

use \AcceptanceTester;
use Page\Login as LoginPage;

class MyCest {
    protected $login_page;
    public function _before(AcceptanceTester $I) {
      $this->login_page = new LoginPage($I);
    }
}
1

1 Answers

0
votes

It is expected behaviour.

Your LoginPage constructor should not have any arguments to be instantiated during DI, so your workaround is right way to initialize LoginPage instance with AcceptanceTester instance.

When you specify AcceptanceTester as LoginPage's ctor arg DI mechanism of Codeception tries resolve dependencies recursively in the following way:

LoginPage(AcceptanceTester) -> AcceptanceTester(Scenario) -> Scenario(TestCase) -> TestCase

but TestCase is abstract class so it can not be instantiated.