5

I've created the second unit-test for my second class. The second class will take a dependency of a first class instance and in one test I'd like to use the object manager to create the second class.

How do I manage that my unit-test gets the object manager injected in Magento 2 so that I can use it?

1 Answer 1

11

You can use in your unit test an instance of \Magento\Framework\TestFramework\Unit\Helper\ObjectManager.

/**
 * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
 */
protected $objectManager;
public function setUp()
{
    parent::setUp();
    ....//your mocks go here
    $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
}

Then you can use this object manager to instantiate your other class:

$this->objectManager->getObject('Your\Other\Class\Here');
1
  • 1
    Thanks a lot Marius for the fast reply. It does not inject, but obtains it from state: $subject = (new ObjectManager($this))->getObject(ConfiguredFile::class); - works in my case.
    – hakre
    Jan 6, 2016 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.