use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class MyUnitTest extends \PHPUnit_Framework_TestCase { protected $objectManager; public function setUp() { $this->objectManager = new ObjectManager($this); } public function testMyFunction() { $myClass = $this->objectManager->getObject('My\Class\Name'); $result = $myClass->myFunction(); $this->assertEquals('expected result', $result); } }
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use My\Module\Model\MyModel; class MyUnitTest extends \PHPUnit_Framework_TestCase { protected $objectManager; public function setUp() { $objectManager = new ObjectManager($this); $this->myModelMock = $this->getMockBuilder(MyModel::class) ->disableOriginalConstructor() ->getMock(); $this->objectManager->addSharedInstance($this->myModelMock, MyModel::class); } public function testMyFunction() { $myClass = $this->objectManager->getObject('My\Class\Name'); $result = $myClass->myFunction(); $this->assertEquals('expected result', $result); } }In the first example, we create an `ObjectManager` instance and use it to instantiate an object of the class `My\Class\Name`. We then call a method on that object and check that the result is as expected. In the second example, we create an `ObjectManager` instance and then create a mock object of the class `My\Module\Model\MyModel`. We add this mock object to the object manager's shared instances so that any objects that are instantiated with the object manager will receive this mock object when they request an instance of `My\Module\Model\MyModel`. The `Magento\Framework\TestFramework\Unit\Helper\ObjectManager` class is part of the Magento\Framework\TestFramework package library.