【问题标题】:Unable to get mock container to return anything but null无法让模拟容器返回除 null 之外的任何内容
【发布时间】:2021-12-31 01:04:43
【问题描述】:

由于无法成功存根控制器的get 方法,我有一个测试失败:

1) Tests\my-project\BackendBundle\Service\PdfGenerateServiceTest::test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null
TypeError: Argument 1 passed to Mock_Pdf_d0288d34::setLogger() must implement interface Psr\Log\LoggerInterface, null given, called in /var/www/my-project/src/my-project/BackendBundle/Service/PdfGenerateService.php on line 66

测试

    public function test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null()
    {
        $protocolAndHost = "http://foo.bar.com";
        $service = new PdfGenerateService($this->createFileServiceMock(), $this->createTranslatorMock(), $this->createSnappyMock(), $this->createContainerMock(), $protocolAndHost);
        $httpPathToPtImage = $service->getHttpPathToPtImage(null);

        self::assertEquals($httpPathToPtImage, $protocolAndHost . "abc/def");
    }

失败的构造函数

    public function __construct(FileService $fileService, Translator $translator, Pdf $snappy, ContainerInterface $container, string $protocolAndHost)
    {
        $this->fileService = $fileService;
        $this->translator = $translator;
        $this->currentLocale = $this->translator->getLocale();

        /* Could reconfigure the service using `service.yml` to pass these in using DI */
        $this->twig = $container->get('twig');
        $this->logger = $container->get('logger');  // <--- should not be null

        $timeoutInSeconds = 15; // can be high, since the job is done async in a job (user does not wait)
        $snappy->setLogger($this->logger); // <--- THIS FAILS due to $this->logger being null

存根

    protected function createContainerMock()
    {
        $containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
        $loggerMock = $this->createLoggerMock();
        $containerMock->method('get')->will($this->returnValueMap([
            ['logger', $loggerMock]
        ]));
        return $containerMock;
    }

我真的不明白为什么当我使用上面的 returnValueMap 调用设置要返回的模拟时,get('logger') 调用只返回 null


偶然,我刚刚发现了一个关于这个主题的 SO 问题,someone mentioned 你需要提供所有参数,甚至是可选参数。然后我查看了界面,它确实列出了第二个参数:

public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);

不过,将地图更改为 ['logger', null, $loggerMock] 并没有改变,所以我有点不知所措。


Phpunit 6.5、PHP 7.2、Symfony 3.4

【问题讨论】:

    标签: symfony phpunit symfony-3.4


    【解决方案1】:

    你真的很接近解决方案。在为returnValueMap 中的可选参数提供值时,您必须使用该值本身,而不仅仅是 null。

    所以不是

    ['logger', null, $loggerMock]
    

    尝试指定

    ['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
    

    完整的调用如下所示:

    $containerMock->method('get')->will($this->returnValueMap([
       ['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
    ]));
    

    【讨论】:

    • 已验证工作。谢谢,新年快乐:)
    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多