我使用了返回类型声明,我将嘲讽更新到版本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的实例。
我做错了什么或者我需要在错误报告中解决这个问题吗?
答案 0 :(得分:1)
使用带有效类名的别名前缀可以解决此错误。如下所示:
$m = m::mock('alias:ITunesScrapper');
更多信息可在官方文档http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#aliasing
中找到