Ecomdev_PHPUnit, Controller tests and core/index/noCookies
I tried to test an observer which should kill the form_key on the add to cart
action. To do this I wanted to put a product into the cart and make sure it was added, e.g. by testing that checkout_cart_add_product_complete
was dispatched.
But my test responded with:
Failed asserting that request route matches expected one.
Expected :checkout/cart/add
Actual :core/index/noCookies
The problem is, that magento tries in \Mage_Core_Controller_Varien_Action::preDispatch
to make sure, that it is allowed to set cookies. It is reading the cookie $cookies = Mage::getSingleton('core/cookie')->get();
and checked whether there is something in it: if (empty($cookies))
. In $cookies
should be at least the frontend
session id.
To make sure, that core/cookie
returns an non-empty array, we mock it:
$sessionId = 'sessionId1234';
$coreCookieMock = $this->getModelMock('core/cookie', ['get'], false, [], [], false);
$coreCookieMock->method('get')->willReturn(['frontend' => $sessionId]);
$this->replaceByMock('singleton', 'core/cookie', $coreCookieMock);