我只是想知道,我正在尝试创建这个测试用例,但是它一直失败。使用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次。
想知道,
谢谢!
答案 0 :(得分:1)
假设您要测试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();
}
}
现在在条形测试中,您会说,运行barAction()
时,您会期望调用fooAction()
并返回模拟结果:
$fooMock = Mockery::mock(Foo::class);
$barTest = new Bar($fooMock);
$fooMock->shouldReceive('fooAction')->andReturn('mockReturn');
$result = $barTest->barAction();
$this->assertEquals('mockReturn', $result);
在该示例中,我在构造函数级别传递了Foo对象,但是如果在函数级别传递,则其作用相同
我希望这会有所帮助!