In the case where you can’t edit a local version of your APIs in order to cause them to fail, you need to be able to edit your client so that it looks like the API request fails. Here’s a quick code change that should make this easy for you.
This is one of many possible manual testing techniques. This allow you to see the change in your running app as you work through it. For instance, I like to use this as I’m testing through the error alerting features that are common in an app when an API request fails.
Go to the point in your app where your bits touch the network. This is usually the place where you use your HTTP wrapper or, if you’re a free spirit, call some XHR goodness yourself. These days, I like using the venerable axios library. Axios is promise based, so that means that we can use async/await as well, which is like a fine chocolate next to a warm fire in winter (which is good).
So, all my http code might look in essence like this:
1 2 3 4 5 6 7 8 9 10 11
request function returns a Promise. The logic around the request might look like:
1 2 3 4 5 6 7 8 9 10 11 12
To make this request fail, we need only make the Promise fail, rejecting it like a mouthful of stewed tomatoes. So, just change
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Now note that you must be aware of what your format HTTP library, in this case axios, uses in its responses. What you
reject manually must be exactly the same format that usually is returned in an error situation (eg,
data). You must also know what your server is designed to return as an error response (eg,
errors). The above application code is using the JSON API format.
What other little tricks do you find useful in getting APIs to fail?