[英]Laravel Mockery Failing Test
我只是想知道,我正在尝试创建这个测试用例,但是它一直失败。 使用Mockery是否有我缺少的东西?
/** @test */
function can_store_podcast_thumbnail()
{
$podcast = factory(Podcast::class)->make([
'feed_thumbnail_location' => 'https://media.simplecast.com/podcast/image/279/1413649662-artwork.jpg',
]);
$mockedService = Mockery::mock(\App\PodcastUploadService::class);
$mockedService->shouldReceive('storePodcastThumbnail')
->with($podcast)
->once()
->andReturn(true);
$podcastUploadService = new \App\PodcastUploadService();
$podcastUploadService->storePodcastThumbnail($podcast);
}
这是我得到的错误:
Mockery\Exception\InvalidCountException: Method storePodcastThumbnail(object(App\Podcast)) from Mockery_2_App_PodcastUploadService should be called
正好1次,但称为0次。
就是想,
谢谢!
假设您要测试Bar类,该类取决于Foo类:
/**
* This is the Dependency class
*/
class Foo
{
public function fooAction()
{
return 'foo action';
}
}
/*
* This is the class you want to test, which depends on Foo
*/
class Bar
{
private $foo;
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
public function barAction()
{
return $this->foo->fooAction();
}
}
现在在bar测试中,您会说,当您运行barAction()
您期望fooAction()
被调用并返回模拟结果:
$fooMock = Mockery::mock(Foo::class);
$barTest = new Bar($fooMock);
$fooMock->shouldReceive('fooAction')->andReturn('mockReturn');
$result = $barTest->barAction();
$this->assertEquals('mockReturn', $result);
在该示例中,我在构造函数级别传递了Foo对象,但是如果在函数级别传递它,其工作原理相同
我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.