【问题标题】:mockery with PHP 7 and return types嘲弄 PHP 7 和返回类型
【发布时间】:2018-10-31 22:17:04
【问题描述】:

我正在使用返回类型声明,并且我将 mockery 更新到 1.1 版取决于 this post,但它仍然无法正常工作。

我有一个带有方法的工厂:

public function getScrapperByUrl($type):AppScrapperInterface
{
    $this->validate($type);
    switch ($type) {
        case self::ITUNES:
            return app(ITunesScrapper::class);
            break;
        case self::PLAYSTORE:
            return app(PlayStoreScrapper::class);
            break;
        default:
            throw new AppScraperException("Can't scrap info");
    }
}

在测试中,我在嘲笑 iTunesScrapper 的行为:

$m = m::mock(ITunesScrapper::class);
    $scrapedInfo = [
        'name' => "comico",
        'downloads' => ""
    ];
    $m->shouldReceive('getOfferAnchor')->with(m::any())->andReturn($scrapedInfo['name']);
    $m->shouldReceive('getOfferDownloads')->with(m::any())->andReturn($scrapedInfo['downloads']);
    App::instance(ITunesScrapper::class, $m);

我收到这样的错误

TypeError: Scrappers\ScrapperFactory::getScrapperByUrl() 的返回值必须是 Scrappers\AppScrapperInterface 的实例,Mockery_2__Adgate_Components_AppstoreFetchers_Itunes_ITunesScrapper 的实例返回。

我做错了什么还是我需要在错误报告中解决这个问题?

【问题讨论】:

    标签: php php-7.1 mockery


    【解决方案1】:

    可以通过使用具有有效类名的别名前缀来解决此错误。像下面这样:

    $m = m::mock('alias:ITunesScrapper');
    

    更多信息可以查看官方文档http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#aliasing

    【讨论】:

      猜你喜欢
      • 2016-06-22
      • 1970-01-01
      • 2018-12-24
      • 2015-06-21
      • 2020-09-14
      • 1970-01-01
      • 2016-08-01
      • 2017-09-01
      • 2014-01-16
      相关资源
      最近更新 更多