Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MizouziE/d66aeddd5abc783da142ce117a5f29ea to your computer and use it in GitHub Desktop.
Save MizouziE/d66aeddd5abc783da142ce117a5f29ea to your computer and use it in GitHub Desktop.
How to test that a job dispatches another job in Laravel
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class FirstJobTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_asks_every_employee_to_rate_the_given_manager(): void
{
// The FirstJob() dispatched another job, called SecondJob().
// We need to omit this job from the Queue facade's fake method so we can assert that
// the second job was pushed.
Queue::fake()->except([FirstJob::class]);
// Now we may dispatch the first job normally
FirstJob::dispatch();
// Having been allowed to operate normally, the first job will push second job to the fake queue.
Queue::assertPushed(SecondJob::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment