11 votes
Accepted

How to properly use getCollectionMock

TL;DR You are using the getCollectionMock() method correctly. The problem is that it only mocks the getIterator() method. Your code however calls toOptionArray() on the collection, not getIterator(). ...
Vinai's user avatar
  • 14k
5 votes
Accepted

Magento 2 - PHPUnit using mocking or without mocking?

It looks to me that you're mixing up 2 types of testing here: unit testing and integration testing. Unit Testing With unit testing, you're testing a single unit. Now what the definition of a "Unit" ...
Giel Berkers's user avatar
  • 12.2k
3 votes
Accepted

Magento 2 : mocking `getParam()` in unit test not working

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 ...
Marius's user avatar
  • 198k
2 votes
Accepted

Proper way to mock a collection

OK. the answer was so obvious but I missed it somehow. What I needed to do is to check what exactly Magento\Framework\TestFramework\Unit\Helper\ObjectManager::getCollectionMock() does. And this is how ...
Marius's user avatar
  • 198k
1 vote

Mocked shared instance incomplete on second identical integration test

I can't say what exactly causes this, but using @magentoAppIsolation enabled works around the problem.
Eino Keskitalo's user avatar
1 vote

How to create mock for factory classes?

You don't have to generate a factory for tests, just mock it even if the class doesn't exist: // pass class name as plain string $factoryMock = $this->getMockBuilder('\Magento\Framework\Controller\...
xpoback's user avatar
  • 140
1 vote

How to create mock for factory classes?

You can do it like this, $rowFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class) ->disableOriginalConstructor() ->getMock(); ...
Keyur Shah's user avatar
1 vote
Accepted

Magento 2: Mocking method, return value not working

You created a mock ($learningHelper), but did not do anything with it. The object manager contains lots of magic, but not enough to know what you do in your test setup and what's your intention ;-) ...
Fabian Schmengler's user avatar
1 vote
Accepted

How to mock an external resource in Magento 2?

You assign the mock to a local variable $skuManager and then do nothing with it. When instantiating your component under test, you have to pass the mock as constructor parameter in place of the real ...
Fabian Schmengler's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible