Object Manager helper

Block and model class constructors declare many dependencies. The Magento system uses constructor dependency injection .

To unit test such classes, you must manually create mocks for all constructor parameters before you can instantiate the class objects. If the number of dependencies is ten or greater, this task is time-consuming.

Use the \Magento\TestFramework\Helper\ObjectManager helper class to simplify this task. Its methods automatically create mocks for all required dependencies. You can then instantiate a testing object by passing these mocks to a class constructor.

You can still create your custom mocks, if needed.

Do not use the ObjectManager helper class for classes with a small number of dependencies.

ObjectManager methods

The ObjectManager public interface methods are:

getObject method

Creates mocks for all constructor dependencies and applies any specified custom mocks from $arguments array.

Also, instantiates the required $className by using constructor with already existing mocks.


public function getObject($className,
     array $arguments = array());


$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);

// default constructor arguments
$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool');

// custom constructor arguments
$cacheMock = $this->getMock('\Magento\Cache\FrontendInterface');
$arguments = array('cache' => $cacheMock);
$scopePool = $objectManagerHelper->getObject('\Magento\App\Config\ScopePool',

getCollectionMock method

Retrieves a collection instance with mocked getIterator method.


public function getCollectionMock($className,
     array $data);

The collection contains elements from the $data array.


$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
// Prepare mock for collection elements
$option = $this->getMock(
    array('getSelections', '__wakeup', 'getData'),
$optionCollection =

getConstructArguments method

Lists dependency mocks for a specified class.


public function getConstructArguments($className,
     array $arguments = array());

In the Magento system, several tests introduced mocks for abstract models and blocks.


$attributeData = array(
    'store_label' => 'Test',
    'attribute_code' => 'test',
    'is_required' => 1,
    'validate_rules' => array(
        'min_text_length' => 0,
        'max_text_length' => 0,
        'input_validation' => 0,
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
$attributeClass = '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute';
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
// Retrieve mocked constructor arguments
$arguments = $objectManagerHelper->getConstructArguments(
        'data' => $attributeData,

/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit\Framework\MockObject\MockObject */
$attribute = $this->getMockForAbstractClass($attributeClass,