Skip to content

Prepare Your Classes to Work in Simulated or Real Modes

In many of our development projects, if not all, we are confronted with situations when we need to test our classes before the final deployment of a project. I’m not talking about Unit Testing here, though I highly recommend the excellent session on that topic from XDC 2018.

For example, it would not be desirable to send hundreds of emails to all the entries in a database simply to test one of the workflow steps or to verify that emails are being delivered as expected. It would be a lot simpler, and less disruptive to those using your app, to test using a few email addresses that are under your control.

So let’s establish a mechanism that allows us tell our apps when to run in a “simulated” mode vs. a “real” mode for all or some of the components that we need to test along the development cycle.

In fact, the way we can implement this is very simple and is based in the definition of a Boolean property in every class (let’s call it Simulation) that we can make visible under the Inspector Panel using the Inspector Behavior feature. Doing this, we can also change it when we drag and drop the class on the Window Layout tray in order to create an implicit instance.

In association with the property itself, we will implement every public method that will be called from the consumer (other component or code of the app) as usual, with the only difference that we will add two additional private methods for every public method we want to run in both modes: simulated and real. For example, let’s say we have a class that returns a Xojo.Core.Dictionary array with data from a database. We create a public method for the class, called GetData, but when we just need to test other components it provides some fake data instead of connecting and retrieving a bunch of real data from the remote database. In this case we could define the following methods for the class:

  • GetData() As Xojo.Core.Dictionary(). Public Method, part of the visible interface for the rest of the app.
  • GetRealData As Xojo.Core.Dictionary(). Private Method, called if the Simulation property is set to False.
  • GetSimulationData As Xojo.Core.Dictionary(). Private Method, called when the Simulation property is set to True.

Then, the real code for our public method will be simply this:

Return if(Simulation = True and DebugBuild = True, GetSimulationData, GetRealData)

Simple, isn’t it? With this you will have the maximum flexibility to choose the functionality you want to test using real or simulated data: all, none, part of them … even when you deploy the compiled project it will work always with real data.

Javier Rodri­guez has been the Xojo Spanish Evangelist since 2008, he’s also a Developer, Consultant and Trainer who has be using Xojo since 1998. He manages and is the developer behind the GuancheMOS plug-in for Xojo Developers, Markdown Parser for Xojo, HTMLColorizer for Xojo and the Snippery app, among others

*Read this post in Spanish