简体   繁体   中英

Accurate usage of mockery PHPUnit?

I was just wondering, Is this an accurate representation of how to use Mockery with PHPUnit or can you think of a better example? In essence I am mocking my PodcastUploadService as I am not concerned that a podcast is uploaded but rather that the method is called and returns true for my inputs.

    <?php
/** @test */
function can_store_podcast_thumbnail()
{
 $podcast = factory(Podcast::class)->make([
     'feed_thumbnail_location' => 
     'https://media.simplecast.com/image/artwork.jpg',
 ]);

 $mockedService = Mockery::mock(\App\PodcastUploadService::class);
 $mockedService->shouldReceive('storePodcastThumbnail')
    ->with($podcast)
    ->once()
    ->andReturn(true);
 $mockedService->storePodcastThumbnail($podcast);
}

Just wondering,

Thanks!

I would argue that if you aren't at all concerned whether a podcast is uploaded, you shouldn't write a test for it.

But then again, if you really aren't concerned with it, you shouldn't write code for it either.

In the current form, your test doesn't add a lot of value. All it's testing, really, is whether the mocking framework works. That's already tested elsewhere, though, so you're doing double work.

A mock would make sense when you'd be testing a service that depends on the PodcastUploadService: you only want to test that service and not need to deal with the PodcastUploadService itself.

For more information, this article about Mocks may be of help.

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