objectManager = new ObjectManager($this); $this->checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class); $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class); $this->object = $this->objectManager->getObject( \Magento\Checkout\Observer\LoadCustomerQuoteObserver::class, [ 'checkoutSession' => $this->checkoutSession, 'messageManager' => $this->messageManager ] ); } public function testLoadCustomerQuoteThrowingCoreException() { $this->checkoutSession->expects($this->once())->method('loadCustomerQuote')->willThrowException( new \Magento\Framework\Exception\LocalizedException(__('Message')) ); $this->messageManager->expects($this->once())->method('addErrorMessage')->with('Message'); $observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class) ->disableOriginalConstructor() ->getMock(); $this->object->execute($observerMock); } public function testLoadCustomerQuoteThrowingException() { $exception = new \Exception('Message'); $this->checkoutSession->expects($this->once())->method('loadCustomerQuote')->will( $this->throwException($exception) ); $this->messageManager->expects($this->once())->method('addExceptionMessage') ->with($exception, 'Load customer quote error'); $observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class) ->disableOriginalConstructor() ->getMock(); $this->object->execute($observerMock); } }