Unit Testing and Test Doubles in Swift

Çağatay Emekci
Mobile Development
Published in
4 min readMar 29, 2020

Testing is a process that verifying and validating your code. Unit testing is a way of testing your code. No matter which software language you use to develop a project, you should be heard of Unit testing. The assumption of “A programmer who knows how to write code can also write unit test code” is wrong. Sometimes you use it without understanding exactly what the function or class will do in itself but if you want to write a test for a function or class, you need to understand exactly what they are going to do inside.

In this article, I will explain that “What is Unit Testing?”, “Why should we use it?”, and “What is Test Doubles?”. While I explain these, I will give examples using Swift. Well, let’s start, what is Unit Testing?

According to Wikipedia;

In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.

Why should we use it?

By writing unit tests, we can test the functionality of our code whether it is working as expected or not. In addition, there are many advantages to writing unit tests as follows.

  • It is very important to software quality.
  • It can help to drive good design.
  • It allows you safe refactoring.

Now, we know about Unit testing and we should use it.

Test Doubles

While we are writing unit tests, we want to use objects that behave like their production equivalents but are a simplified version. We can call this kind of object a Test Double. External dependencies are managed with Test Doubles and easy to simulate various scenarios.

Test Double is a generic term for any case where you replace a production object for testing purposes. — Martin Fowler

There are 5 types of Test Doubles. Each one has a different purpose. We will dive deep into types of test doubles.

  • Dummy
  • Fake
  • Stub
  • Mock
  • Spy

Dummies

Dummy objects are objects that never used. This means it is not being used in a test and only act as a placeholder. Dummy objects also use to satisfy method parameters. You can implement to dummy object very easily.

In this example, we want to create a Provider object. For creating a Provider object, we have to pass the NotificationProvider variable on the initializer. We can use the dummy object in there.

Fakes

A fake is an object that has working implementations that replicate the behavior, but not the same as the production one.

In this example, imagine that we have a protocol that adds a notification to the database and gets a notification by using an identifier. We use this database only for production. So, we need to have a fake object to replicate the behavior. For this purpose, an example code is given below.

Stubs

Stub is an object which will always return a set of predefined data. Stub can provide “canned responses”.

Imagine that, we need Notifications for displaying. “getNotifications” method in NotificationGetter protocol can provide notifications. Therefore, while creating NotificationStub, it should be conformed to NotificationGetter protocol. Now, “getNotifications” in NotificationStub can provide notifications as a stub.

Mocks

Mocks are objects that register calls they receive. They also track which method being called and how many times it was called. Mock object types are good for verifying exact behavior.

As in the example above, we conform NotificationGetter for mock objects and then we will be tracking “getNotification” method.

Spy

Spy records interactions and “verify” later. Spies are very similar to mocks and the opposite of stubs. When your code under test performs a side effect based on a dependency, you can use spies to record the effect.

If we want to test whether notification preference is enabled or not, we will write the code as below.

Conclusion

Unit testing can check the exact behavior of each piece in the code. Therefore, test Doubles are very useful when you are testing your code. They can reduce the complexity and dependency of a test. Also, they enable us to enhance the quality of the software.

Further Readings

Test Double — Martin Fowler
Test Double — xUnit Patterns
Mocks Aren’t Stubs — Martin Fowler

--

--

Çağatay Emekci
Mobile Development

iOS Application Developer, Opsgenie at Atlassian @CagatayEmekci