Question

I have this Magento 2 unit test code:

    $storeInterfaceMockDe = $this->getMockBuilder(\Magento\Store\Model\Store::class)
        ->disableOriginalConstructor()
        ->setMethods(['getLocaleCode'])
        ->getMock();
    $storeInterfaceMockDe->expects($this->any())->method('getLocalCode')->willReturn('de_DE');

But PHPUnit throws:

Trying to configure method "getLocalCode" which cannot be configured because it does not exist, has not been specified, is final, or is static

But the core uses similar code.

What am I doing wrong?

Actually the method is a magic method, so it really does not exist -- but why does it work in the core code?

Was it helpful?

Solution

You have a typo in the method name. The mocked method is getLocaleCode (with an e in Locale), and the expectation is set for a method getLocalCode (without the e).

Small side note, somewhat unrelated to your question:
I don't like partial mocks, so I generally also add the existing methods to the mock if I have to configure magic methods via the builder:

->setMethods(array_merge(get_class_methods(Store::class), ['getLocaleCode']))
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top