Test Mail and number of emails send

Posted 3 months ago by longestdrive

Hi

My Laravel 5.5 testing journey continues

How do I test how many emails were sent by my test.

I'm trying to test that multiple emails were sent in a test. The method I'm testing against is only sending to the first recipient so I need to find a way of building a test

My current test is this:

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

    Mail::fake();

    factory(Player::class, 4)->create();

    $players = Player::get()->pluck('id')->toArray();

    $request = [
        'recipients'=>$players,
        'subject'=>"test subject",
        'message'=>'test message'
    ];

    $response = $this->actingAs($this->user)
        ->call('POST', 'broadcast/create', $request);

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

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

I don't know where I should count() the emails sent. H

How do I build my test correctly?

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

Reply to

Use Markdown with GitHub-flavored code blocks.