Creating Mocks in LabVIEW

In my last article, I introduced the concept of Mock Objects. The obvious next question is how do you implement them in LabVIEW? Many other languages either have builtin or readily available third party mock objects frameworks. LabVIEW does not. Until Now.

I’ve create some tools for class refactoring that includes some tools for making Mock Objects. When you install the latest package, you will see some new options in the Tools Menu.

Here are the new tools menu options.

Copy Class and Move Class

These 2 options allow you to copy or move/rename classes. But wait! I can already do that in LabVIEW. Yes you can. You can right click on a class in the project and click save as. That will allow you to create a copy or move/rename the class. However that does not rename all the class controls on all the vis in the class. These 2 new tools options have scripting code to do that.

Create Interface

The Create Interface Tool allows you to take an existing class and create a parent class for it that acts like an interface. That parent class will simply include all the dynamic dispatch methods of the existing class, but they will all be empty. If you remember back to my discussions on Test Doubles, using Test Doubles requires coding to an interface. This tooll will create that interface for you. Very useful for refactoring existing code.

Create Mock Object

The Create Mock Object tool allows you to take a class (presumably an interface-like class) and make a child class of it that is a Mock Object. Remember that a mock object simply records each method call along with the parameters and then at the end has some way to verify that the method calls and parameters are correct. The exact details of what this does will be explained in the next blog post.

The GUI

The GUI for each of these methods looks exactly the same. It’s rather simple. You first choose a base class. Then you set the destination path for the generated code. You can decide if you want the generated code to be added to the project. Click Ok and it will generate the code for you.

More Information

Stay tuned for the next article where I will delve into how the Mock Objects are able to capture the method calls and parameters. If you can’t wait, here is a little spoiler. It’s demo I recorded of how the whole process works.