Mock Mailable with parameters

Posted 2 months ago by pashaster

Hi to everyone! I'm trying to test the mail was received with this code:

// Assert a message was sent to the given users...
        Mail::assertSent(BalanceChanged::class, function ($mail) use ($user, $wallets) {            
            return $mail->hasTo($user->email) &&
                   $mail->hasFrom(env('MAIL_USERNAME')) && 
                   $mail->hasMarkdown('emails.balance_changed') &&
                   $mail->hasWith([
                       'wallets' => $wallets
                   ]);
        });

But my assertion failed. I think it's about of my Mailable class has two input params which I don't know how to pass them while asserting:

namespace App\Mail;

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

use App\User;

class BalanceChanged extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The receiver instance.
     *
     * @var User
     */
    public $receiver;
    
    /**
     * Array with the wallets information
     *
     * @var Array
     */
    public $wallets;
    
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $receiver, $wallets)
    {
        $this->receiver = $receiver;
        $this->wallets = $wallets;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from(env('MAIL_USERNAME'))
                ->to($this->receiver->email)
                ->markdown('emails.balance_changed')
                ->with([
                    'wallets' => $this->wallets
                ]);
    }
}

What's wrong?

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.