The Best of Both Worlds

Unit Testing With Mock Network Calls

Yes, you can test your asynchronous code and own your data at the same time.

Kenny Dubroff
The Startup
Published in
4 min readJun 27, 2020

--

Mock Data Loader
Read more to learn how to leverage me

Unit testing is a great way to ensure your app works across many iterations. Sometimes it’s important to ensure things work asynchronously, but also to ensure that you have consistent data. When you don’t control your remote source, this can be virtually impossible.

The problem with unit testing in a dynamic environment

If you're unit testing a network call, chances are that you aren’t just testing to make sure that you can connect to the network, but to ensure that you can do something meaningful with it, and perhaps to check that something happens afterwards. That something afterwards usually relies on consistent data.

For example, I want to connect to server A, receive some data, decode the data, then test to make sure the data contains a specific value. That’s an easy enough unit test to write, but there’s an inherent problem in the nature of networking. Things change — and that’s normally a good thing. But when you’re unit testing, you need to rely on things staying the same (such as that specific value from earlier).

--

--

Kenny Dubroff
The Startup

I’m a Full Stack iOS Developer with a passion for writing clean, reusable code.