简体   繁体   中英

Laravel Mockery Failing Test

I was just wondering, I am trying to create this testcase however it keeps failing. Is there something that I am missing with using 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);
}

This is the error that I am getting:

Mockery\Exception\InvalidCountException: Method storePodcastThumbnail(object(App\Podcast)) from Mockery_2_App_PodcastUploadService should be called

exactly 1 times but called 0 times.

Just wondering,

Thanks!

Assume you want to test the Bar class, which depends on the 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();
    }
}

Now in your bar test, you will say that when you run barAction() you expect that fooAction() is called and return a mock result:

$fooMock = Mockery::mock(Foo::class);
$barTest = new Bar($fooMock);

$fooMock->shouldReceive('fooAction')->andReturn('mockReturn');

$result = $barTest->barAction();

$this->assertEquals('mockReturn', $result);

In the example I passed the Foo object at the constructor level, but works the same if you pass it at function level

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM