0

I have a piece of code that looks like this

$noRouteIdentifier = $this->scopeConfig->getValue(
                    'web/default/cms_no_route',
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                );
                $noRouteDelimiterPosition = strrpos($noRouteIdentifier, '|');
                if ($noRouteDelimiterPosition) {
                    $noRouteIdentifier = substr($noRouteIdentifier, 0, $noRouteDelimiterPosition);
                }
                $customRedirectionUrl = $this->url->getUrl($noRouteIdentifier);
                $this->_messageManager->addError("invalid contact form");
                return $this->resultRedirectFactory->create()->setPath($customRedirectionUrl);

How I can mock data for the last line return $this->resultRedirectFactory->create()->setPath($customRedirectionUrl);

the $this->resultRedirectFactory belongs to \Magento\Framework\Controller\Result\RedirectFactory class

I have tried mock it in setUp() like this but it not work

It returns an error: Method name matcher is already defined, cannot redefine

$this->resultRedirectFactory =
            $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
                ->disableOriginalConstructor()->getMock();

        $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
            ->disableOriginalConstructor()->getMock();

        $setPath = $this->resultRedirectFactory->expects($this->any())
            ->method('create')
            ->willReturn($this->resultRedirect);

        $setPath->method('setPath')->willReturn('/');

the constructer

// SUT
        $this->model = $this->objectManager->getObject(
            Index::class,
            [
                'context' => $this->contextMock,
                'customerSession' => $this->session,
                'resultPageFactory' => $resultPageFactoryMock,
                'resultRedirectFactory' => $this->resultRedirectFactory,
                'resultRedirect' => $this->resultRedirect
            ]
        );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.