Testing Queued Listeners

LaravelPosted on

When we fire an event, a listener possibly handles quite much work behind the scenes, so often we queue them. Laravel offers this by default, all we need to do to implement the ShouldQueue contract and pull in the InteractsWithQueue trait. The rest is up to Laravel.

But when it comes to testing, things are a bit more tricky. According to the documentation, it’s easy to test our queued jobs. But what about the listeners? Using this snippet, we can test if the listener is pushed to the queue or not:

use App\Listeners\SendWelcomeEmail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Events\CallQueuedListener;

// Test setup and so on...
Queue::fake();

// Perform the thing that fires the events
Queue::assertPushed(CallQueuedListener::class, function ($job) {
    return $job->class === SendWelcomeEmail::class;
});

Need a web developer? Maybe we can help, get in touch!

Similar Posts

More content in Laravel category