Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

asmerkin's avatar

How to test a Queued Closure

Hi,

I'm learning to work using TDD and Laravel. Everything is going great so far, but now I have a doubt about how to test a Queued Closure is dispatched.

Any clues?

0 likes
3 replies
bobbybouwmann's avatar

Laravel has a lot of helpers for testing your jobs. For example

// ActionJobTest.php
Bus::fake();

// Perform your action

Bus::assertDispatched(ActionJob::class, function ($job) {
    return $job->data['action'] === 'deleteAllFiles';
});

However, this doesn't support closure based jobs.

My recommendation would be creating a job class, which makes this more testable as well ;)

asmerkin's avatar

Does it make sense if I'm only delaying a single model method?

bobbybouwmann's avatar

I would always create a class for a job. This will give you a clear view of all the jobs you have as well in your app/Jobs directory. You can't reuse it as a callback as well.

Please or to participate in this conversation.