Anatomy of a Mock Object

Anatomy of a Mock Object

In the last post, I talked about Mock Objects and what they are. In this post, I am going to talk a little bit about what they look like when you implement them in LabVIEW. The video below shows how to use the Class Refactoring Toolkit I created to create and use a Mock Object.

You can get the latest copy of the tools at https://gitlab.com/sas_public/class_refactoring_tools. You should note that I added code to automatically call the icon editor, so you no longer have to do that manually.

Class hierarchy showing how Mock and Production Class inherit from the same interface.

The first thing to note can be seen in the class hierarchy. The Mock inherits from the same interface class as your production code. That means that as long as the rest of your code is written to the Interface class, you can easily swap in the Mock during testing and use the production class the time. Having an interface class also allows you to do simulation, so it is a really useful technique to know.

Private Data of a Mock Object.
Private Data of a Mock Object.
Project View of Mock Object Helper
Project View of Mock Object Helper.
Initialize Mock Helper creates the initial reference.
Private Data of Mock Object Helper.

The next thing to look at is the private data of a Mock Object. It consists simply of a Mock Object Helper Object. This object is responsible for logging all the calls and verifying them at the end. If you open that class up in the project, you will notice it only has a few methods. It is worth noting that its private class data consists of a reference, which is created in the Initialize Method. Since Mock Object Helper is a by-reference class, the Mock Object also becomes a by-reference class. This is important because it allows us to pass the Mock Object off to the code we want to test and we don’t have to retrieve it at the end. We can simply fork the wire before we do the dependency injection.

Initialize Mock Helper creates the initial Mock Object Helper.
Verify Mock Log calls the Verify method on the Helper.

You will notice the Mock has inherited all of the methods of its parent interface. If you pay close attention you will also notice that it has 2 additional methods. The Initialize Mock Helper method must be called before you do the dependency injection and before you fork the wire. This creates the Mock Object Helper. The Verify Mock Log is the method you call at the end of your test to verify that the code you are testing has used the Mock Object correctly. It is also worth noting that the Verify Method also cleans up all the references (since they are no longer needed). It should be the last thing you call on the Mock Object.

Typical Method on Mock Object.

If you look at all the methods that Mock Object inherits from its parent interface class, you will see they all look similar. They are empty except for a call to the Log Call Method of the Mock Object Helper. Any parameters are simply bundled together and passed in as a cluster. The Mock Object Helper takes care of logging the method call and the parameters.