“Mock it until you fake it” – Sounds like a good title for a book on unit testing huh? (If you are nerdy enough to get the gist of it.)
This is a short introduction to Unit Testing and Mocking in layman terms. Just so you get the idea and concept. Nothing too complex more or less a way of thinking than anything else.
The art of unit testing is mainly about units. As simply as that and appears so obvious. But so many tests turn into something far from that. Far from a unit test more of a monster of integration tests. But before I lose you I wish you to meet Ronan.
Here’s Ronan with his piece of cake. He seems happy enough with it. Until he discovers a lump of garlic inside his delicious pie. Something has gone terrible astray – very unexpected indeed. He is no longer a happy camper at all.
Not being miffed about the fact his pie tastes rather peculiar. He wonders when and how ?
The When And The How
The pie was prepared and made up of pastry and apple filling then topped off nicely with some icing sugar when it was fully baked.
The question of how and when did the garlic clove enter into the pie can only be answered by testing at various stages. Similar to a bug entering into code within an application.
So each of the components needs to be checked and their ingredients too. Just like in any quality control system there should be measures at each stage. Whether it be building and baking of a pie or a coding an app. Both have expected behaviour and expected responses of satisfaction.
Hence quality control of code is of the utmost importance.
In staying with our analogy of the apple pie. It would be best to validate each main components. But to do that requires sticking our finger into the mixture or licking the spoon. In professional terms probing and measuring with sensors.
Meaning each processes before and after should have a result. These processes are units and can be tested. The smaller and more contained our processes are the easier it is to test.
We would catch the lump of garlic before it arrived on Ronan’s plate.
Units are important small pieces which all together ensure the final result is just how it should be – expected.
Take our apple filling for example. An apple pie must have filling to be an apple pie right? I guess so, I hear you answer. However in the land of coding we would fake the pastry so we could get at the filling. To taste and test it. Ensuring no weird scent of garlic or other things exist. This is what is called a “code smell” in the world of programming. Something stinks or isn’t how it should be!
If you like to read more on Code Smell check out this blog blog : https://blog.codinghorror.com/code-smells/
Learning how to mock or fake what you need – just the right amount, mind you – is the art of unit testing.
Surround your subject/object by encapsulating the code to fool it into thinking that everything exists. The code feels fine and secure, believing everything is right with the world.
It’s kind of like suspending the apple filling in mid air without it falling to the floor. Mocks are simply supports to keep the filling from falling.
However you must remember to keep the apple filling as real as possible without altering it’s consistency or its ingredients. Just like in code. It’s got to work in real terms and in the testable realm too. Otherwise there’s literally no proof in the pudding so to speak!
Once you’ve managed to fake the pastry you can get right into it. Using your probing spoon to verify the filling is just right. Without the fear of crumbles or other factors which might influence the flavour and taste of the filling. This is how programming code is tested in units by mock what is needed to get right into it!
I hope this small blog post, helps to clarify how unit testing and mocking work kind of work. I tried very much to simplify the concept and process as much as possible.
By the way Ronan didn’t really have garlic in his pie. Far as I know he was quite satisfied with his Apple pie indeed.