- A stub method is a method that just returns a simple but valid (though not necessarily correct) result .
BASIC FLOW DIAGRAM
WHY/WHERE DO WE USE MOCK API
- Start building your application even before getting the real API .You would be knowing what all would be the API responses or if you don’t know just agree on the API’s proposed design and mock it accordingly. So start implementing even before the backend team starts writing the API.
- Will enable you to work even when the server is down, during network issues, or even travel . In another way of saying, you can work even if you are offline.
- You can experiment how your app works in different scenarios . This can make your application crash-proof even for weird responses from API.
- Another place where stubs are used is in mocking when testing, You supply stub methods that the code you are testing relies upon through dependency injection that return fixed results and then ensure that the code does the right thing with them. This isolates testing to the code you are trying to test and means you don’t need to spin up a database just to run tests.
HOW TO DO?
Before starting on how to do, let’s discuss terms, which I will be using here so you can understand better .
Imagine you have an application to fetch the repos of user by entering thier name,(you can download the example project from here.-Objective c). In this case, you would have a fetchGithubRepos api which fetches the repos of the user from github corresponding to the parameters passed. So here you will have four clases,(Note: You can give any name to these classes.)
- Your main class , for example FetchReposViewController class.
- APIManager class — where you have all the api methods and functions which you use in your application.
- APIController class — where you have the same api method names as in your APIManager class , except the method functions are different .
- MockAPI class — where you store the mock response
Here i am using a BOOL in the APIController class to decide ,which response to use — original api response or mock api response .(Note:You can use other methods to decide which response to use.)
Now lets go to how the api stubbing works.
Lets take an example here for easy understanding. In normal scenarios, to fetch the user repos we call the “fetchGithubRepos” API (APIManager class).For that we pass parameters such as visibility ,type,sort etc to the API , and the API request is send to the server. On receiving the request, the server sends back a response to the API, which include the repos of that particular user and other useful parameters. Since we are only showing the name of the repos, we find the “key” having the name of the repos and display it in the app .
This works well fine, as long as the server is up and working good all the time, but if you are developer you will by the time knowing that things don’t always work like that . Many times server will be down, there will be network issues and other issues which will hinder you from proceeding with doing your job peacefully.
This is where mock API response parts come in. Let’s take the earlier mentioned scenario itself for explaining how it works .
As usual we call the “fetchGithubRepos” API,but here instead of calling the API method in the APIManager class,we call the API in the APIController class. In the APIController class, on checking the “BOOL”, we decide whether to use the mock response in the “MockAPI” class or to call the “fetchGithubRepos” API method in the APIManager class. The advantage of it is that, irrespective of whether the server is down or there is network issues, the “fetchGithubRepos” api will get the response — mock response or real response .
The mock response can be attained by taking the response from the login api and storing it in a string. (Note:Try to store in encrypted format). This is easy if the response in a JSON format. Sometimes the response will be as object, here it will be a little tricky, as you cannot simply copy/paste the response from the api in a string as you did with JSON format. Here you have to create a class for example ““fetchGithubReposMockObject”” and create objects which match with the response from the api and init each objects with possible values. Once this is completed , you can pass this object as response of mock API .
This post is for a basic understanding of how the api stubbing works. Try start doing it with a small project having one or two API. After getting a basic understanding go for the big one .
For more in depth and advanced version of mock API’s, stubs , do refer these .