Setup of a Clean Swift pattern helps writing unit tests
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…