🇺🇸 Builder Pattern applied to Unit Testing

When writing a sofware, you as a software developer, must write something to ensure the quality of your code. If we are dealing with test driven development, it is a good practice having your unit tests well implemented, because in the future, as the software evolves, you will have to look into your tests, understand them, modify them to acomplish your job.

It is very common make an special effort to write your unit tests in a good readable way.

In addition to this effort, following a suggestion from Steve Smith, I implemented the builder pattern applied with unit testing to see the results. With a combination of: Microsoft Unit Test, C#, FluentAssertions and the builder pattern, I could see a great test in a simple scenario and would like to share with y’all.

First I created a simple class named Employee with the properties: name, birth date and gross salary. Additionally I added a behavior to calculate the net salary.

The second step is to implement your Employee builder:

And finally just write your unit tests tying those methods together and assert the expected result at your will:

Afterall, you will have a good readable code, without much additional effort.

To see the complete code, use the following git repository.

This article is also available in Portuguese.

Software Engineer and passionate about distributed systems

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