Laravel Mail assertSent and assertQueued don't work in tests

Viewed 475

I'm trying to test email sending on my Laravel 8 project, but the Mail::assertQueued() and Mail::assertSent() methods don't seem to work at all.

This is the test:

public function test_send_simple(): void
{
    Mail::fake();

    $test_subject = $this->faker->text(25);
    ScheduledEmail::factory()->create(["status" => "queue", "subject" => $test_subject]);

    $command = $this->artisan("emails:send");

    $command->assertExitCode(0);
    Mail::assertQueued(SendableEmail::class);
}

The test uses an intermediate object to hold the mail (which makes a SendableEmail instance internally), and then calls the emails:send command, which sends all emails.

When I try this manually, it works, but when I test it, any assertion with Mail's methods fail. I'm unsure what to try next or why it wouldn't work.

Here are the relevant parts of my env.testing:

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database  # I also tried sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MAIL_MAILER=log
1 Answers

Maybe you need to explicitly use run() method. just like below.

$command = $this->artisan("emails:send")->run();
Related