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().
...
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" ...
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 ...
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 ...
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.
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\...
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();
...
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 ;-)
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mocks × 9magento2 × 7
unit-tests × 6
phpunit × 4
collection × 2
testing × 2
php × 1
magento2.4 × 1
plugin × 1
performance × 1
object-manager × 1
factory × 1
integration-test × 1
continuous-integration × 1
magento2.4.5 × 1