Templating

Take a look at how the final email is templated and how to test that the templating is done correctly.

We'll cover the following

Our last task before tying it all up is templating. This will be a simple section with limited tests, but we’ll still manage with properties. The requirement is straightforward. Send an email with the text:

Happy birthday, dear $first_name!.

The tests

The function should take one employee term, and that’s it. Since the focus is on unit tests and we won’t send actual emails, only templating needs coverage for now. Let’s start by writing a property in a standalone suite. It has been written in the code widget below in the test/prop_bday_mail_tpl.erl file as the property prop_template_email and the generator employee_map.

The string:find/2 function looks for a given string within another one and returns it if found, or nomatch if missing. We’ll see that some fields are defined as nullable in the employee module. The initial specification did not mention if it were possible or not for them to be missing, but since the sample of two entries all had fields, we’ll assume that our production data and our generators reflect that fact.

The implementation

The implementation has been done in the src/bday_mail_tpl.erl file in the code below.

A trivially correct convenience function that extracts all needed for an email to be sent (address, subject, body) can be added to further decoupling. This would be the spec full(bday_employee:employee()) function.

Get hands-on with 1200+ tech skills courses.