How to test if a job has been queued?

Viewed 29

I wrote this code to test, if a job has been queued after submitting a result.

The chain of happenings is, that an event triggers a listener and the listener dispatches the job.

public function test_certificate_can_be_created_after_submitting_test_result()
    {

        $user = User::factory()->create();
        $test = Test::factory()->create();

        $response = $this->actingAs($user)
            ->put(route('tests.update', $test->id), [
                'result' => 'positive',
                'result_time' => '2021-01-01 12:00:00',
            ]);

        Event::fake([
            TestResultSubmitted::class,
        ]);
        Queue::fake();

        event(new TestResultSubmitted($test));
        Event::assertDispatched(TestResultSubmitted::class);
        Event::assertListening(
            TestResultSubmitted::class,
            ListenersCreateCertificate::class
        );

        Queue::assertPushed(CreateCertificate::class);

        $response->assertStatus(302);
    }

The event is just an event as it was created by php artisan make:Event TestResultSubmitted.

The Listener has just one line in the handle() function:

JobsCreateCertificate::dispatch($event->test);

The JobsCreateCertificate is empty atm. The only change I made is, that it implements ShouldQueue`.

The test result is: The expected [App\Jobs\CreateCertificate] job was not pushed.

When I add a dd(1) into the handle() function of a job, the result of the test is 1. So the handle function is triggered.

0 Answers
Related