Testing Laravel Mailables and passing data

Posted 1 week ago by longestdrive

I'm trying to test email for a Laravel 5.5 app - that uses Mailables. I'm not sure how I go about testing when the Mailable receives data as the examples I've seen don't seem to include data.

Here is the test I'm developing:

    public function testBroadcastMailable()
    {
    $this->disableExceptionHandling();

    Mail::fake();

    $subject = "test subject";
    $message = "test message";

    Mail::assertSent(NewBroadcastMessage::class, function ($mail) use ($subject, $message) {
        $mail->build();
        return $mail->subject === $subject;
    });
    }

The assertion currently fails.

This is the Mailable class:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class NewBroadcastMessage extends Mailable
{
use Queueable, SerializesModels;
public $message;
public $subject;

/**
 * Create a new message instance.
 *
 * @param $subject
 * @param $message
 */
public function __construct($subject, $message)
{
    //
    $this->message = $message;
    $this->subject = $subject;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->markdown('messages.newBroadcastMessage')
        ->subject($this->subject);
}
}

What is the correct way to pass data in a test and test mailables?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.