When deciding whether to use Bus::fake() or Queue::fake() in your tests, it's important to understand the context in which each should be used.
Bus::fake() is used for faking the dispatching of jobs, events, and commands. This is useful when you want to ensure that a job or command is dispatched without actually executing its logic. It's also the way to go when you're dealing with job chains or batches, as the Bus facade is responsible for handling these advanced job features.
Queue::fake(), on the other hand, is specifically for faking queue jobs. When you're only interested in testing the queuing of jobs and not the other features of the Bus facade, Queue::fake() is the appropriate choice.
Here's how you can use each in your tests:
Using Bus::fake():
use Illuminate\Support\Facades\Bus;
use App\Jobs\ProcessPodcast;
public function test_podcast_process_is_dispatched()
{
Bus::fake();
// Perform order shipping...
// Assert that a job was dispatched...
Bus::assertDispatched(ProcessPodcast::class);
// Assert that a job was not dispatched...
Bus::assertNotDispatched(AnotherJob::class);
}
Using Queue::fake():
use Illuminate\Support\Facades\Queue;
use App\Jobs\ProcessPodcast;
public function test_podcast_process_is_queued()
{
Queue::fake();
// Perform order shipping...
// Assert that a job was pushed onto the queue...
Queue::assertPushed(ProcessPodcast::class);
// Assert that a job was not pushed onto the queue...
Queue::assertNotPushed(AnotherJob::class);
}
In summary, if you're testing job dispatching in general, including job chains and batches, you should use Bus::fake(). If you're only interested in testing the queuing of jobs, Queue::fake() is sufficient.
Since the expectsJobs and doesntExpectJobs methods are no longer supported, you would replace them with Bus::assertDispatched and Bus::assertNotDispatched respectively when using Bus::fake(), or with Queue::assertPushed and Queue::assertNotPushed when using Queue::fake().
Choose the one that best fits the scope of what you're trying to test in your Laravel application.