Skip to content

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

pcmagas profile image Dimitrios Desyllas github logo ・1 min read

Can you help me on how I can assert that a job actually dispatches an another one?

I have the following Laravel Worker:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;

use App\Lobs\AnotherJob;

class MyWorker implements ShouldQueue
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;

    public function handle(): void

And I want to unit-test that my job dispatches the AnotherJob:

twitter logo DISCUSS (1)
markdown guide

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()
Classic DEV Post from Aug 4 '19

You're not worth hiring unless...

Dimitrios Desyllas profile image
Greedings I am Dimitrios Desyllas aka pc_magas. I am a php software engineer in e-table. I am interested in electron, C++, privacy enhancing terchnologies and cryptography