Setup of a Clean Swift pattern helps writing unit tests

Jeroen de Vrind
Short Swift Stories
6 min readFeb 15, 2019

--

Photo by Frances Gunn on Unsplash

Do you know these moments that you start an app convincing yourself you need to certify quality by writing as much unit tests as you can? Until that moment you find out it’s almost impossible or takes so much time to write the proper unit tests. Well I had these moments and started realising that a good pattern in your app helps writing unit tests.

A good pattern comes first

In order to write better unit tests you need a consistent pattern. The MVC-pattern is definitely not the best pattern if you want to write better unit tests. Most of the time you’ll end up with a very large ViewController that has all kinds of jobs in it: a Massive View Controller.

In a good pattern each component acts as a black box independent of any other component. What’s going on inside the black box you don’t know and you don’t care. But these black boxes should have well defined inputs and outputs. In a consistent structure it should be easier to identify components, test subjects, inputs and outputs.

Code independence is achieved by the use of protocols in Swift. With the use of protocols a component doesn’t own another component directly, but has an indirect reference to that component via a protocol. In this way both components don’t know about each other, but are both…

--

--

Jeroen de Vrind
Short Swift Stories

iOS Engineer at ABN Amro bank. Author of How to develop accessible iOS apps.