mardi 10 mars 2020

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire