Making Unit Test For Sending Email Using Laravel

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:


Written by

Full Stack Developer (Laravel/Vue.js)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store