4
votes

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: <[email protected]>
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.

2

2 Answers

2
votes

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

0
votes

Dusk runs in a separate process, so there is no way you can use Mail::fake() when doing Dusk tests. Actually it is like you are working with your app directly, simply with a test vars.

Here is a way you can test emails when doing Dusk tests by the way - https://medium.com/@naneri/how-to-test-laravel-dusk-emails-826e77c4d53d