Photo by Jordan Harrison on Unsplash

Unit Tests, by definition, are developed to test a single unit of code, in isolation.

Unit Tests should never call out to a ‘real’ network, and i’m here to show you how you can easily stub your network responses so you can continue to test your ‘unit of code’ behaviour upon receipt of pre-defined, stubbed responses.

All of what we want to achieve can be done so using Apple’s trusty, URLSession. More particularly, the URLSession’s configuration property, URLSessionConfiguration. Let’s take a look.

Our URLSessionConfiguration allows us to define additional URLProtocol's which in turn, define the behaviour of requests. …

Photo by Matt Popovich on Unsplash

Here we are going to take a look at one approach we can use to give our View Controllers a different ViewModel for UITests. Afterall, We don’t always care to hit a real service for our UI tests, we just care that it displays correctly when it’s provided with state and/or data change.

So let’s jump in; 🏊‍♂️

Let’s have a View Model for fetching a Todo Item from some web service, yay.

Photo by Taylor Vick on Unsplash

OK, let’s jump straight in. You have a URLSession object, wether its a fully configured one, or the bare-bones, basic, shared singleton, either way you make your request, get your data and then go about your business…. and that’s great, but what if it wasn’t?

At this year’s WWDC we have seen Apple yet again push the boundaries of iOS, macOS, watchOS and tvOS.

I spent some time watching the Platforms State of the Union tech video and have summarised each area within this post. I apologise if I have missed anything, or if I have misinterpreted any aspect.

Let’s begin.

In iOS we have seen the entire UI practically opened up to developers. With iOS10 we, as developers, are now able to interact with rich customisable notifications, create Message Apps and of course, integrate with Siri.

This is mostly achievable via Extensions, using Extensions…

So, turns out that many iOSDev’s are being fooled by what the following should return…

Comparing strings or Memory Address?

Objective C, ok we get it, it’s an Object Orientated programming language dating back some time now and many dev’s would instantly assume that ‘==’ would compare the memory address’ of the 2 strings, instantiated separately and well, they are correct.

So wait hold on, (Thanks @nanasarpong)


Senior iOS Engineer  — Advanced Github downloader — Serial StackOverflow browser

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store