mcbates's avatar

How to assert mail subject and from in test (Laravel v7.8.1)

I fail to assert from and subject in my tests in latest Laravel 7.8.1

use App\Mail\ContactUserMail;
…

public function testExample()
    {
        Mail::fake();
        $this->get('/');

        Mail::assertSent(ContactUserMail::class, function ($mail) {
            dd($mail->subject, $mail->from);
        });
    }
// ContactUserMail

…

public function build()
    {
        return $this->from('[email protected]')->subject('A busy content')->markdown('emails.contactUserMail');
    }
// web.php
Route::get('/', function () {
    Mail::to('[email protected]')->send(new ContactUserMail);
});

This returns null, if I dd() the whole $mail, I only see the to, no from, subject either.

0 likes
1 reply
resohead's avatar

Try calling $mail->build(); inside your mail assertion.

Mail::assertSent(ContactUserMail::class, function ($mail) {
	$mail->build();
	return $mail->hasTo('[email protected]')
	&& $mail->from === '[email protected]'
	&$ $mail->subject === 'A busy content';
});
           	

Please or to participate in this conversation.