📈 Unit Testing in Swift: Asynchronous Expectations

Kewin
The Startup
Published in
5 min readJul 9, 2020

--

In this second part of the Unit Testing in Swift series, we will cover how to properly test asynchronous code using expectations. If you are new to unit testing and want to learn how to start unit testing with Swift in Xcode, I suggest you take a step back to Part 1: The Fundamentals of the series, before reading further. This part of the series is for you, if you want to learn how to properly test the asynchronous code and functionality within your application.

Let’s begin with an example!

Consider the School example from Part 1: The Fundamentals. Only the currently enrolled students of the school are kept in memory, but we wish to extend the School with a function for fetching all previous students (alumni) from a remote server. A function like that could look as such:

Several things can be improved in this function (including the abuse of the singleton design pattern) to allow for easier unit testing, but this will be covered in Part 3: Proper Architecture and Part 4: Mocking of this series. For now, let’s focus on writing…

--

--

Kewin
The Startup

Lead iOS Engineer @ Jabra 🇩🇰 Full time 💻📲 — part time ‍🏊🏼🚴🏼🏃🏻⛷⚽️