DEV Community

How I can test that one Laravel job dispatches an another one on testing?

Dimitrios Desyllas on March 31, 2020

Can you help me on how I can assert that a job actually dispatches an another one? How...
Collapse
 
martyhimmel profile image
Martin Himmel

Posted this answer on StackOverflow, too, but here it is for discussion. 😄 Laravel has queue mocks/fakes that will handle that. Try this:

namespace Tests;

use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Queue;
use App\Jobs\MyWorker;
use App\Jobs\AnotherJob;

class TestMyWorker extends TestCase
{
  public function testDispachesAnotherJob()
  {
    Queue::fake();
    MyWorker::dispatchNow();
    Queue::assertPushed(MyWorker::class);
    Queue::assertPushed(AnotherJob::class);
  }
}