Laravel 嘲笑失败测试
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 class,这取决于 Foo class:
/**
* 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()
被调用并且 return 一个模拟结果:
$fooMock = Mockery::mock(Foo::class);
$barTest = new Bar($fooMock);
$fooMock->shouldReceive('fooAction')->andReturn('mockReturn');
$result = $barTest->barAction();
$this->assertEquals('mockReturn', $result);
在示例中,我在构造函数级别传递了 Foo 对象,但如果您在函数级别传递它,效果相同
希望对您有所帮助!
我只是想知道,我正在尝试创建这个测试用例,但它总是失败。使用 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 class,这取决于 Foo class:
/**
* 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()
被调用并且 return 一个模拟结果:
$fooMock = Mockery::mock(Foo::class);
$barTest = new Bar($fooMock);
$fooMock->shouldReceive('fooAction')->andReturn('mockReturn');
$result = $barTest->barAction();
$this->assertEquals('mockReturn', $result);
在示例中,我在构造函数级别传递了 Foo 对象,但如果您在函数级别传递它,效果相同
希望对您有所帮助!