In this article, we'll explore two improved methods to make testing same class dispatched Laravel jobs a breeze. While the standard way gets the job done, it's like searching for a needle in a haystack when something goes wrong. We'll go through two approaches that shed light on exactly what's causing issues, making troubleshooting a whole lot easier. Before we dive in, let's take a quick look at the code we're dealing with: // web.php get('dispatch-jobs'); Queue::assertPushed(TestJob::class, 2); // be aware this callable is called twice // and passes if at least one job returns true Queue::assertPushed(function (TestJob $job) { return $job->name === 'John' && $job->age === 30 && $job->email === 'john@example.com'; }); Queue::assertPushed(function (TestJob $job) { return $job->name === 'Will' && $job->age === 20 && $job->email === 'will@example.com'; }); }}
So, the takeaway is this: while these testing approaches might not be the fanciest and for everybody, they sure help when you're testing dispatched Laravel jobs.