Ad

How To Test That A Job Is Released After A Certain Time When Failing?

- 1 answer

When I have a job like this

class CheckVideoStatusJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $video;

    public function __construct(Video $video)
    {
        $this->video = $video;
    }

    public function handle(CheckStatusAction $action)
    {
        if (! $action->execute($this->video)) {
            $this->release(60);
        }
    }
}

How can I test: when the job has failed it will be released back to the queue after (60) seconds?

Ad

Answer

In my opinion you rely on Laravels own tests to secure release method works and in a work environment not use time testing this. If you still want to, you just need to be sure that release is called. I would do a partial mock using Mockery.

This code creates a class that only mocks the release method and pass the video to the constructor.

$mock = \Mockery::mock(CheckVideoStatusJob::class, $video)->makePartial();

// expects release call, once, parameter should be 60 returns null
$mock->shouldReceive('release')->once()->with(60)->return(null);

// action should offcourse secure that execute returns false
$mock->handle($action);

For securing the tests use assertions with mockery use the mockery trait.

use MockeryPHPUnitIntegration;
Ad
source: stackoverflow.com
Ad