In unit testing I want to create mock object for factory classes e.g.


but I have following error,

generation does not exist.

So how to generate factory class in in generation when create unit testing.

2 Answers 2


You can do it like this,

$rowFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)

        $rawResult = $this


First, you need to create mock of RawFactory class then you need to create mock Raw class and last you need to assign $rawResult to $rowFactoryMock


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\Result\RawFactory')
// set your object as return value

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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