0

I'm trying to send a test email and assert (using events) that it was sent.

The test runs fine and the event in fact happen (I'm logging the event handler method on LogSendingMessage class), but the expectsEvents never assert, neither before, neither after the Mail::send method.

public function testSendSimpleMailOverSMTP()
{
    // Send methods
    $fromMail = 'my@mail.com';
    $fromName = 'Tiago Gouvêa';
    $toMail = 'myanother@mail.com';
    $toName = 'Tiago Gouvêa';
    $subject = 'Mail testing123';

    $view = 'SimpleMail';
    $data = array('body' => "Olá fulano");

    $this->expectsEvents(Illuminate\Mail\Events\MessageSending::class);
    $this->expectsEvents(Illuminate\Mail\Events\MessageSent::class);

    Mail::send($view, $data, function (Illuminate\Mail\Message $message) use ($toMail, $toName, $fromMail, $fromName, $subject) {
        $message->to($toMail, $toName)->subject($subject);
        $message->from($fromMail, $fromName);
    });
}

Here on stack has another questions some like, but, none of then worked for me. I appreciate any help. :)

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
  • Have you tried using `Event::assertDispatched(Illuminate\Mail\Events\MessageSending::class);` ? – Salvatore Q Zeroastro Sep 24 '18 at 19:23
  • @SalvatoreQZeroastro I cannot find the right import for Event class. What's its namespace? – Tiago Gouvêa Sep 26 '18 at 12:43
  • `Event` is the standard event facade, `Illuminate\Support\Facades\Event` - you have to run `Event::fake()` before the test, to swap it with `EventFake` (a support facade for testing - the swap is implicit when running the method) – Salvatore Q Zeroastro Sep 26 '18 at 13:16
  • @SalvatoreQZeroastro Ok, I tried like you said but I'm keeping getting "The expected [Illuminate\Mail\Events\MessageSending] event was not dispatched" – Tiago Gouvêa Sep 27 '18 at 21:01

0 Answers0