My system sends a couple of important emails. What is the best way to unit test that?
I see you can put it in pretend mode and it goes in the log. Is there something to check that?
My system sends a couple of important emails. What is the best way to unit test that?
I see you can put it in pretend mode and it goes in the log. Is there something to check that?
There are two options.
Option 1 - Mock the mail facade to test the mail is being sent. Something like this would work:
Option 2 is much easier - it is to test the actual SMTP using MailCatcher.me. Basically you can send SMTP emails, and 'test' the email that is actually sent. Laracasts has a great lesson on how to use it as part of your Laravel testing here.
For Laravel 5.4 check
Mail::fake()
: https://laravel.com/docs/5.4/mocking#mail-fakeI think that inspecting the log is not the good way to go.
You may want to take a look at how you can mock the Mail facade and check that it receives a call with some parameters.
"Option 1" from "@The Shift Exchange" is not working in Laravel 5.1, so here is modified version using Proxied Partial Mock:
If any one is using docker as there development environment I end up solving this by:
Setup
.env
config/mail.php
(I had a conflict with this environment variable for some reason)
Carrying on...
docker-compose.ymal
app/Http/Controllers/SomeController.php
app/Mail/SomeMail.php
resources/views/mail/SomeMail.blade.php
Testing
tests\Feature\EmailTest.php
(This has been copied and edited from my own code so might not work first time)
(source: https://stackoverflow.com/a/52177526/563247)
If you just don't want the e-mails be really send, you can turn off them using the "Mail::pretend(true)"