In this tutorial, you will learn how to use AutoMocker to speed up your development effort and reduce the amount of boilerplate test code you will be required to write. Adopting this code will make the future you, love you 😍 The tips contained in this tutorial are super simple, however, they can save you some serious development effort within your project. Let us start by looking at a simple example of where this code saving occurs:
Boilerplate Test Code
Imagine you had this dependency:
This dependency is injected into this class:
To unit test this class, you could write some code like this:
In this simple example, the code to mock the dependency and set it up is trivial. It takes time and effort to write. If the code changes, it will also take added effort to maintain it. If you are working on a project with hundreds of classes, this will result in developers having to write a setup code. Wouldn't it be great if we could use a tool to automatically do this for you? This is where AutoMocker comes into play. AutoMocker will automatically create mocked objects for all constructor parameters automagically 😍😍😍
Using AutoMocker is great in terms of future maintenance. Using normal mocking, if you changed the constructor signature, any resulting tests would break. If you are not specifically testing the new dependencies, tests that are not related to the change may break. Refactoring constructor parameters in these situations takes up time! You will need to invest some time to fix these broken tests before you can recompile the solution again 😠. When using an auto mocker, the test class will be less decoupled from the production class. In most instances, you can add and remove constructor parameters without breaking other tests 💥
AutoFixture
I have written previously about my love of AutoFixture (see here). AutoFixture can be used to auto-populate data into mocked objects. Combining AutoFixture and AutoMocker, you can further reduce the amount of set-up code you need to write. You will need to write some code to make them work together nicely. When using AutoMocker you will get a mocked object. Out-of-the-box, AutoFixture expects to create the object for you as well. What we need is some swanky code that allows AutoFixture to map objects directly 😲 This code looks like this:
This code will auto-mock the constructor parameters, it will then add mock data into that object. Combining these two things will make you a legend in the office. You have now saved your team from having to write a maintain a lot of crappy code 💥
Manually Setting AutoMocked Properties
Even though everything has been auto-mocked, you can still set up and configure the values manually yourself. There are a few ways of doing this, one option is like this:
The same can also be done on a class level and is achieved like this:
As you can see, using these auto mocking solutions saves you from having to write boiler-plate code, however, you can still have the control to define any value you require explicitly if you want. It's the best of both worlds! Happy Coding 🤘