Queue::assertPushed() not working

Published 2 months ago by Nospoon

I have a job that's being dispatched with SendFax::dispatchNow($fax), i can confirm it being dispatched by running dd() from inside the job, however when I try to test it with Queue:fake() and then \Queue::assertPushed(SendFax::class), it claims it didn't push, the test fails with The expected [App\Jobs\SendFax] job was not pushed. I tried changing dispatchNow() to dispatch() also but that didn't help. Any ideas what could be wrong?

bobbybouwmann

Mmh this should work just fine according to my tests and the docs! What version of Laravel do you use? How does the surrounding code looks like? Are you sure the job is fired somewhere?

Nospoon

Sorry, can't post the surrounding code here, but it is irrelevant to the issue either way. I've actually stumbled across the same exact problem wit \Mail::fake() today, I can verify code inside a mailable is being executed but \Mail::assertSent claims it was sent 0 times.

bobbybouwmann

Can you show your test at least?

Anything, you need to make sure you call Queue::fake before you do anything else in your test

public function testSomething()
{
    // Fake everything here     
    Queue::fake();
    
    // Perform your scenario
    $response = $this->get('some/url');

    // Assert stuff
    $response->assertStatus(200);
}

If you do the bake after your perform the scenario, the fake is too late!

Note: If this is not the problem, you really need to show more information!

Nospoon

I am calling the fake method on Queue as the first thing in the test.

/** @test */
    public function it_pushes_send_job_to_the_queue()
    {
        \Queue::fake();
        $user = factory(User::class)->create();
        factory(FaxApplication::class)->states(['with_cover', 'default'])->create([
            'user_id' => $user->id,
        ]);
        $mail = storage_path('testing/mail/mail_simple');
        $channel = new Channel();

        $asteriskClientMock = \Mockery::mock(AriClient::class, ['originate' => $channel]);
        $this->instance(AsteriskClientInterface::class, $asteriskClientMock);

        \Artisan::call('test:email', ['file' => $mail]);

        \Queue::assertPushed(SendFax::class);
    }

The test:email command is relaying the test file into another artisan command responsible for all the processing. Here's the bit inside it that's pushing to the queue:

foreach ($destinations as $destination) {
    $fax = $this->faxService->generateFax($destination, $attachments, $email);

    if ($fax) {
        SendFax::dispatchNow($fax);
        $queuedFaxIds[] = $fax->id;
    }
 }

Please sign in or create an account to participate in this conversation.