Mocking objects in Swift — fake name, last name or description

Sayler8182
Mac O’Clock
Published in
3 min readJun 1, 2020

--

Photo by Leone Venter on Unsplash

Mocking is a technique when we replace real data in an application with some generated or static fake data to handle a specific test case.

In this article, I’ll try to show You how to create and use simple service to Mock data (not the services).

Why do we want to mock something? Mocking is pretty useful when we write tests or when we don’t have complete Backend but we know the structure or even when we develop UI and we need some filling. Using mock generator we can easily change mocking behavior and — whats more important — find all mocked data to avoid fuckup on a production environment.

Let’s start from the end

Ok, let’s assume that we want to create an object with a bunch of properties and all of them should be generated uniquely but also should have some sense. We don’t want situations when in name field we find random hash or in phone number some ‘lorem ipsum’ string. Data should resemble something real.
This is the way how I see that case:

Let me introduce my concept

Of course, there is no ‘randomName’ function in swift. We should prepare a bunch of base properties which holds our real data. With the help of this data, we can select random name or words that make sense. Imagine that You want to generate only German names or Chinese, just change base array and You will get what you need. At this moment names, last names and paragraphs lets us extract real information.

Let’s start with something simpler

In most cases, you need just a simple string. Imagine you have a list of products and you want to generate and check how it will look like with dozen products with different names. Let’s create an extension for our service that returns random word from paragraphs (you can also prepare a separate list with product names).

But hey! What if we have longer names, not only one word or some of our products doesn’t have a name? Go deeper. What if we want to generate longer description? We don’t want to create a new function for each property. Our generator should be reusable for different cases.

The solution is within reach. We can add parameters that extend our methods. Let’s define some options:
- none — we don’t want to modify behavior
- length — we want to specify specific length — short, regular or long
- nullable — we want our value to have a chance to be null

Nullable value

Imagine you need a value to be null but not always e.g. you noticed that in 90% cases your product description is set but there is a possibility that someone skipped it in the input form. You want to take this into account and check what will happen. The function below extract proper option and provide a flag if the value should be null or shouldn’t.

Let's come back to our ‘string generator’ and add options handling. Line number 4 checks if the value should be null. If ‘nullable’ option isn’t set it’ll skip. Then — depends on the length — We can return a string with different length. It’s easy like that.

Let’s consider other properties

The name generator is even simpler. In all methods, we should check the ‘nullable’ option, but length can be skipped. Of course, nothing can stop you from extending it in the case when you have a very long name.

To complete this example we need one more thing. We have to inform our app that model can be mocked. If the model conforms to the Mockable protocol it means that it can generate a mock object.

Endless possibilities…

You probably noticed that our first example uses much more functions than string or name. In the same way, you can create methods to generate numbers, email, postal code or date. Even the whole object can be mocked in another object.

Let me know if you want to see complete code from my mock.framework.

Thank you for reading! If you liked this article, please clap so other people can read it too. I will also have more motivation for next article :) You can also see my other articles, maybe you will find something for you.

If you have any question or suggestion leave a comment.

--

--