How to Mock Responses in Swift with TermiNetwork

Bill Panagiotopoulos
4 min readJan 23, 2021
Photo by Marvin Meyer

How many times have you thought of an idea for implementing an iOS application that requires networking, only to abandon it because you lacked the resources for API implementation? By resources, I mean time, knowledge of a back-end programming language, and/or server equipment. Perhaps you needed to create an application prototype with a very tight deadline as requested by your client, who would then decide whether to proceed with its full implementation. In any case, the idea of mocking responses for an application built on uncertain grounds saves you from investing too much time and effort. (Time is valuable, right?)

If you’ve found yourself in a position like this and never moved forward 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 yet read the ‘Getting Started with TermiNetwork’ guide, it is recommended that you start there to familiarize yourself with the library.

Before delving into the technical aspects and steps for mocking responses, let’s explore various scenarios where you might need to implement mocked responses in your application:

  • You aim to create an application prototype or a completely offline application.
  • You wish to support an offline mode in your application, enabling it to function in both online and offline modes.
  • You need to mock a specific response for which the API has not yet been implemented on the back-end.
  • You prefer to concentrate on the UI/UX and functionality implementation first, before addressing networking concerns.
  • You intend to write tests that utilize mock data.

Mock Requests with TermiNetwork

Now that you understand the various scenarios, let’s explore how TermiNetwork supports them. We will modify an existing Todo application, which already has networking 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 https://github.com/billp/TermiNetworkTodoDemo --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:)
  • apiAddTodo(todo:success:)
  • apiDeleteTodo(todo:success:)
  • apiToggleTodo(todo: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
todos.json

2. generic-success.json

generic-success.json

the final bundle should look like this:

MockData.bundle

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:

TodoRoute.swift

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: https://github.com/billp/TermiNetworkTodoDemo

If you’ve found the TermiNetwork library useful, please visit https://github.com/billp/terminetwork and press the ⭐ star button to make it visible to other iOS developers as well.

Cheers 🍺.

--

--