Making Unit Test For Sending Email Using Laravel

Rafa Rafael
May 15 · 1 min read

On my last post about sending email notification using observer, I forgot to add its test when building it. Our team adopted TDD and although I am not a big fan of it, I slowly liked it and learned how beneficial it is to follow. But since I am not really talking about TDD, you may search its advantages and also why I was not a big fan.

As you can see on my last post that I have two methods hooked on the model events, created and deleted so the test I have created for them is a unit tests.

To create a unit test you run php artisan make:test ClaroomObserverTest --unit and the file can be found in test/Unit.

As you can see on my codes below, I used Mail Fake to prevent mail from being sent And then assert the mailables were sent to the teacher.

Or a refactored version based on my last post about pivot table. The code will look cooler like this:

Enjoy!

Rafa Rafael

Written by

Full Stack Developer (Laravel/Vue.js)