簡體   English   中英

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類,該類取決於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM