devhobby's avatar

Laravel job delay test

Hello

This is my implementation. How can I test this behaviour?

$startTime = Carbon::createFromFormat('Ymd', $dateString)->startOfDay();

if ($startTime->isFuture()) {
    (new FooFileDownloaderJob)
        ->dispatch()
        ->delay($startTime->diffInSeconds());
}
0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@devhobby This could be a possible scenario:

    public function testJobIsDispatchedForFutureDates()
    {
        // Fake the queue
        Queue::fake();

        // Call the method with a future date
        $this->scheduleDownload(now()->addDay()->format('Ymd'));

        // Assert the job was pushed to the queue
        Queue::assertPushed(FooFileDownloaderJob::class, function ($job) {
            // Assert the job is scheduled for the correct date
            return $job->date->isNextDay();
        });
    }

Please don't forget to write the test for the opposite case (if you feel).

3 likes

Please or to participate in this conversation.