laravellaravel-5laravel-5.4browser-testinglaravel-dusk

Mocking mail with Laravel 5.4 (dusk)


I'm trying to test mails within browser tests in Laravel 5.4.

I have this code in a InvitationTest class inside a browse callback function:

\Mail::fake();

$browser->visit('projects')
    ->press('etc.')

\Mail::assertSent(InvitationMail::class, function ($mail) use ($project) {
    return $mail->invitation->project->id == $project->id;
});

I can see the mail was sent in logs:

[2017-04-06 15:36:10] local.INFO: Invitation xxxx-yyyy-zzz-aa-bb started
[2017-04-06 15:36:10] local.DEBUG: Message-ID: <xxx@yyy.dev>
Date: Thu, 06 Apr 2017 15:36:10 +0200
Subject: hello world :)

But running php artisan dusk I got:

1) Tests\Browser\InvitationTest::testCreateInvitation
The expected [App\Mail\InvitationMail] mailable was not sent.
Failed asserting that false is true.

Mail is queued but, in my .env.dusk.local I have set queue to sync (and mail to log):

QUEUE_DRIVER=sync
MAIL_DRIVER=log

So everything must be fine. Am I doing something wrong? Why dusk says the mail was not sent?

Important note: The mail is not directly send via Controller, but a delayed job is created and this job send the mail. The delayed job is sent in the past, so it may not have an impact. To be clear: a controller dispatches a job, then this job sends multiple mails.


Solution

  • Unfortunately, mocking does not work for Laravel Dusk. Though, two issue are open about this problem: