Home > Software engineering >  Laravel implement assertSentFrom specific address Mailable Testing
Laravel implement assertSentFrom specific address Mailable Testing

Time:04-28

Trying to get to grips with Mocking and test cases, I want to test that a Mailable TestMail is sent from [email protected], the documentation provides hasTo, hasCc, and hasBcc but doesn't look like it uses something like hasFrom. Is there any solutions to this?

https://laravel.com/docs/9.x/mocking#mail-fake

public function testEmailAlwaysFrom()
{

    Mail::fake();

    Mail::to('[email protected]')->send(new TestMail);

    Mail::assertSent(TestMail::class, function ($mail) {
        return assertEquals('[email protected]', $mail->getFrom());
        // return $mail->hasTo($user->email) &&
        //     $mail->hasCc('...') &&
        //     $mail->hasBcc('...');
    });

}

CodePudding user response:

MailFake doesn't provide hasFrom method in the class and therefore will return false.

The workaround below however doesn't work when using the environmental variable MAIL_FROM_ADDRESS, ->from() has to be called within build().

A couple of GitHub issues have been reported suggesting a workaround below:

https://github.com/laravel/framework/issues/20056 https://github.com/laravel/framework/issues/20059

public function testEmailAlwaysFrom()
{

    Mail::fake();

    Mail::to('[email protected]')
        ->send(new TestMail);

    Mail::assertSent(TestMail::class, function ($mail) {

        $mail->build(); // <-- workaround
    
        return $mail->hasTo('[email protected]') and
            $mail->hasFrom('[email protected]');
        
    });
        
}
  • Related