简体   繁体   中英

Laravel unable to test job with mocked Service and object. The Mockery spy fails to detect called method

I want to test my functionality of my Job:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use App\Model\Model;
use App\Services\Service;

class MyJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;

    /**
     * Undocumented variable
     *
     * @var User
     */
    private $model;

    public function __construct(Model $model)
    {
        $this->model=$model;
    }

    public function handle(Service $service): void
    {
        if($this->model->shouldCallService){
            $service->method();
        }
    }
}

And I want to test it with a pure unit test:

namespace Tests\Jobs;

use Tests\TestCase;
use Mockery;

use App\Services\MyService;
use App\Jobs\MyJob;
use App\Model\MyModel;

class TestJob extends TestCase
{
    public function testMethod()
    {        
        $this->app->instance(MyModel::class,\Mockery::mock(MyModel::class,function($mock){
            $mock->shouldReceive('save')->andReturn(true);
        }));

        $model=factory(MyModel::class)->create([
            'shouldCallService'=>true,
        ]);

        $mockedNewsLetterService=Mockery::spy(MyService::class);
        $mockedNewsLetterService->shouldReceive('method')->once();

        $job=new MyJob($model);
        $job->handle($mockedNewsLetterService);
        $mockedNewsLetterService->shouldHaveReceived()->method()->with($model);
    }

    public function tearDown() {
        parent::tearDown();
        Mockery::close();
    }
}

But once I test it via:

./vendor/bin/phpunit ./test/Jobs/TestJob.php

I get the following output:

PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 46.12 seconds, Memory: 34.00 MB

There was 1 error:

1) Tests\Jobs\TestJob::testMethod
Mockery\Exception\InvalidCountException: Method method(<Any Arguments>) from Mockery_1_App_Services_MyService should be called
 at least 1 times but called 0 times.

/var/www/html/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/vendor/mockery/mockery/library/Mockery/Expectation.php:312
/var/www/html/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:104
/var/www/html/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:46
/var/www/html/vendor/mockery/mockery/library/Mockery/HigherOrderMessage.php:47
/var/www/html/tests/Jobs/TestJob.php:32

ERRORS!
Tests: 1, Assertions: 3, Errors: 1.

Do you know why the spy fails to detect the call on method method?

You called wrongly the shouldHaveReceived of mockery.

Use this approach:

$mockedNewsLetterService->shouldHaveReceived(`method`)->with($model);

Also, for testing the job dispatching I use the bindMethod of app therefore the test should be:

public function testMethod()
{        
    $this->app->instance(MyModel::class,\Mockery::mock(MyModel::class,function($mock){
        $mock->shouldReceive('save')->andReturn(true);
    }));

    $model=factory(MyModel::class)->create([
        'shouldCallService'=>true,
    ]);

     $mockedNewsLetterService=Mockery::spy(MyService::class);

    $this->app->bindMethod(NewsLetterSubscribeUserJob::class.'@handle', function ($job, $app) use ($mockedNewsLetterService){
        return $job->handle($mockedNewsLetterService);
    });

    MyJob::dispatchNow($model);
    $mockedNewsLetterService->shouldHaveReceived('method')->with($model);
}

As you can see I use the app to let handle job dispatching. Also in order to avoid 3rd party services during testing I use dispatchNow as well in order my job not to be queued.

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