如何模拟上下文对象


8

我正在尝试测试包含上下文对象的模块,如下所示:

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Catalog\Model\Product\ImageFactory $productImageFactory,
    \Magento\Framework\View\Asset\Repository $assetRepo,
    \Magento\Framework\View\ConfigInterface $viewConfig,
    ProductRepository $productRepo
)
    {
    parent::__construct($context, $productImageFactory, $assetRepo, $viewConfig);
    $this->productRepo = $productRepo;
}

当我运行单元测试时,它会显示以下错误消息:

PHPUnit_Framework_Error : Argument 1 passed to Magento\Catalog\Model\Product::__construct() must be an instance of Magento\Framework\Model\Context, none given, called in ...

如何模拟该对象或解决此错误?

Answers:


7

您可以像这样测试您的课程:

public function testSomething() 
{
    $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
         ->disableOriginalConstructor()
         ->getMock();
    $productImageFactoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\ImageFactory::class)
         ->disableOriginalConstructor()
         ->getMock();
    $assetRepoMock = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
         ->disableOriginalConstructor()
         ->getMock();
    $viewConfigMock = $this->getMockBuilder(\Magento\Framework\View\ConfigInterface::class)
         ->disableOriginalConstructor()
         ->getMock();
    $productRepoMock = $this->getMockBuilder(\Magento\Catalog\Model\ProductRepository::class)
         ->disableOriginalConstructor()
         ->getMock();

    $productImageMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Image::class)
         ->disableOriginalConstructor()
         ->getMock();

    //mock here any methods you need to mock

    $productImageFactoryMock->method('create')->willReturn($productImageMock); 

    $model = new \The\Class\You\Want\To\Test( 
               $contextMock,
               $productImageFactoryMock,
               $assetRepoMock,
               $viewConfigMock,
               $productRepoMock
    ); 
    //do asserts here
}

setUp()如果您需要进行多个测试,则可以将所有模拟建筑移至该方法。


谢谢您的好评!但是我仍然不能像代码中提到的那样模拟getSku()。所以我在这里发布:magento.stackexchange.com/questions/122785/…您能帮我一个忙吗?在此先感谢
Amin Memariani

3

以下代码将在测试中创建上下文的模拟。

    $contextMock = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
        ->disableOriginalConstructor()
        ->getMock();

从: vendor/magento/module-sales/Test/Unit/Helper/DataTest.php:setup()

然后,您将需要$contextMock通过对象管理器使用传递:

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$objectToTest = $objectManager->getObject(
        \Magento\Class\To\Test::class, [
            'context' => $contextMock,
            ...
        ]
    );

或者,如果需要更改要更改的类的方法输出,则可以使用Mock并传递参数:

$objectToTest = $this->getMockBuilder(\Magento\Class\To\Test::class)
    ->setConstructorArgs([
        'context' => $contextMock
    ])->getMock();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.