1

I'm breaking my head for hours now over a simple thing I've done hundreds of times: mocking another model. Can anyone see what's wrong with this?

$mockedRequest = $this
    ->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
$mockedRequest
    ->method('getParam')
    ->will($this->returnValueMap([['a', 'b']]));

var_dump($mockedRequest->getParam('a'));

You would expect the result to be b right? WRONG! It returns NULL! And I can't for the love of God figure out why...

If anyone can point me in the right direction I would be so happy... It's probably a small thing, but I can't see it ...

2

The getParam method accepts 2 parameters, the second one defaulting to null

public function getParam($key, $defaultValue = null); 

returnValueMap works like this. the first N-1 values in the array are treated as the parameters for the method and the Nth value is the returned result.
So in your case it looks in the map for an array like this [a, null, return value here].
And you don't have such an sub-array in your array map. So it returns by default null.
make the return map like this:

->will($this->returnValueMap([['a', null, 'b']]));
  • Thanks. I knew it had to be something simple like this. Sounds like a case of code-blindness to me... – Giel Berkers Nov 21 '16 at 22:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.