简体   繁体   中英

How to test Laravel Job with Delay?

I've a job that is being delayed with a dynamic value:

SomeJob::dispatch()->delay($time);

In PHPUnit , we can test if a job has been dispatched or not, but is there any way to check if the job has been dispatched after that specific $time ?

A quick search turned up a number of articles on doing this. Call Queue::fake() and then assert that the job was placed on the queue. Within this assertion, you can confirm that it has a delay.

<?php

namespace Tests\Feature;

use App\Jobs\SomeJob;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    public function testJobDelayed()
    {
        Queue::fake();

        // [...] Run code that dispatches the job

        // Assert that Job was pushed with a delay
        Queue::assertPushed(SomeJob::class, fn ($job) => !is_null($job->delay));
    }
}

Source: https://mauricius.dev/test-laravel-job-delayed-in-queue/

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