How to Mock Responses in Swift with TermiNetwork

Photo by Marvin Meyer

How many times have you found yourself thinking of an idea to implement an iOS application that requires networking but had to abandon because you haven’t had the resources to implement the API? Speaking of resources, I mean time, knowledge of a back-end programming language and/or server equipment. Maybe you have to create an application prototype with a very tight deadline, as requested by your client, who then will decide if you continue to its full implementation. In any case, the idea of mocking responses to an application that has an uncertain ground, saves you from investing in too much of your time and effort. (time is valuable, right?)

Well, if you’ve been in a position like that and never proceeded with the actual implementation due to limitations, this article is for you. TermiNetwork is a full-featured networking library that offers response mocking.

If you haven’t read the Getting started with TermiNetwork guide, it is recommended to start here, to get familiar with the library.

Before we dive into the technical aspects and steps to mock responses, let me show you some different scenarios that you may need to implement mocked responses in your application:

  • You want to create an application prototype or a complete offline application

Mock Requests with TermiNetwork

Now that you’ve got the idea of the different scenarios, let’s see how they are supported by TermiNetwork. We are going to change an existing Todo application which networking is already implemented, to use mock data.

Clone the Sample Project

Make a git clone of the Todo sample project: type the following command in terminal:

git clone --branch start

The Environments, Routes, Requests and Models have been already defined and set up. What we are going to do is to add the mock data files (JSON files) for the existing networking calls, and configure the Environment to use those files instead of making the actual request. Build and run the project:

Network Error

You see a network error, which is correct because there is no server running. Now, take a look to the following functions in TodoViewController.swift

  • apiTodos(success:)

just to get an idea of how the requests are structured. Refer to Getting started with TermiNetwork to see how create Requests with TermiNetwork if you haven’t already.

The point is, without changing any code in the functions above, to mock the responses with the corresponding JSON files.

Create a Bundle with the mock data files

Let’s create a bundle which includes the JSON files, go to File > New > File… and select Settings Bundle, give it a name you want e.g. MockData and click Create.

For the purpose of this Todo application we need to create are two JSON files that will be used as Responses, one for GET /todos route (list of todos), and one for a generic success response, which will be used for add, delete and toggle todo.

Inside the bundle file create the following JSON files:

  1. Todos/todos.json

2. generic-success.json


the final bundle should look like this:


Configure the Mock Data Paths in Routes

Now that we have the mock data bundle set up, it’s time to configure the existing Routes.

Open Router > TodoRoute.swift in Project Navigation and add the mockFilePath parameter to the RouteConfiguration call. For .todos use Todos/todos.json, for .addTodo, .updateTodo, .deleteTodo use generic-success.json.

The final file looks like this:


Configure Environment to use Mock Data

The final step that puts everything together is to configure the environment that have mock responses enabled. Open Environment.swift, scroll down to configuration computed variable and set the mockDataBundle and mockDataEnabled properties.

The final result looks like this:

Configuration computed variable

Build and run the application

Build and run the project and you’ll see the mocked responses in action. You are able to add, delete toggle the state of a todo.

Todos final application with mock data enabled

That’s it 🎉, you’ve just learned how to Mock Responses with TermiNetwork.

The final completed project can be found here:

If you’ve found the TermiNetwork library useful, please visit and press the ⭐ star button to make it visible to other iOS developers as well.

Cheers 🍺.

iOS Software Engineer, Thessaloniki, GR