Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

How to assert a Laravel job is delayed

I've seen a lot of convoluted approaches to this, here's a drop-dead simple way to do it.

Joel Clermont
Joel Clermont
2023-11-01

From our daily Laravel tips email. Get the next one in your inbox.

Tests should give us confidence that our code works as expected. So when we dispatch a job with a delay, it's important to cover that in our tests.

Laravel provides some ways of manipulating time in our tests, but none of those techniques are actually necessary in this case. Instead, we can assert the Job class has the delay property set as expected:

Bus::fake();

ConversationEvent::factory()->create(); // when created, this model dispatches a job with a delay

Bus::assertDispatchedTimes(AddMessageToConversation::class, 1);

Bus::assertDispatched(AddMessageToConversation::class, function (AddMessageToConversation $job) {
    self::assertEquals('some-value-here', $job->someProperty);
    // other property assertions here...
    self::assertEquals(60, $job->delay); // This asserts the job will be delayed 60 seconds
    
    return true;
});

Laravel already has its own tests to make sure it processes the delays properly. There's no need to cover that in your feature tests as well.

Here to help,

Joel

P.S. Do you wish you had more tests in your app? We can help you become more confident with a solid testing strategy.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.