Xcode Dev: save api response for future tests during development - ios

I'm building an app that relies heavily on an api response. The app needs to make this request every time it is launched. However this request takes 60~ seconds. Is there any way to save the response between tests so I can build/test the features that rely on the response?
In short, I don't want to call the server every time I test the app. I'm in swift

What I do is to write conditional code, dependent upon some environment variable, so that I'm not contacting the server but using some constant instead.
Thus, when I'm just testing and developing the rest of the my app, I just throw the environment variable switch, and presto, I'm using that constant.

Related

How can I make API calls from within an Appium and WDIO test?

I am using Appium and Webdriver IO (WDIO).
I need to make API calls to the backend to setup and data and retrieve data during the test.
I can do some of this if I use the before function, but the test fails if I use it in the actual test.
Is it possible to make backend calls during these tests?
FYI, I am using async await and axios.
Without a code sample, it is hard to say what is wrong, but definetly there is a way to make it work.
The first thing to question: are you running wdio in sync mode (default)?
If so, you may have an issue trying to make an API call with async/await (e.g. node-fetch library) in the same test.
Have you tried using browser.call? Check the docs, it basically the way to do what you want with wdio wrapper.
Still failing? Try to increase test suite timeout (e.g. mocha timeout in case of Mocha)

Parse cloud code on iOS

from what I understood Cloud Code is like a server, we can declare functions (only javascript?) to make app running lighter. Should I put every query on Cloud Code instead of iOS app?
You don't have to put every query on Cloud Code but you can use it to host functions that should rest in a sever or you would like to change without requiring a app update.
As a quick example, you can create a function in cloud code to send a mail to a user friend, calculate some number with a variable that you don't know and want to change from time to time, or make some safety check with other servers before a payment.

Mock API Requests Xcode 7 Swift Automated UI Testing

Is there a way to mock requests when writing automated UI tests in Swift 2.0. As far as I am aware the UI tests should be independent of other functionality. Is there a way to mock the response from server requests in order to test the behaviour of the UI dependant on the response. For example, if the server is down, the UI tests should still run. Quick example, for login, mock if password failed then UI should show alert, however, if the login is successful the next page should be shown.
In its current implementation, this is not directly possible with UI Testing. The only interface the framework has directly to the code is through it's launch arguments/environment.
You can have the app look for a specific key or value in this context and switch up some functionality. For example, if the MOCK_REQUESTS key is set, inject a MockableHTTPClient instead of the real HTTPClient in your networking layer. I wrote about setting the parameters and NSHipster has an article on how to read them.
While not ideal, it is technically possible to accomplish what you are looking for with some legwork.
Here's a tutorial on stubbing network data for UI Testing I put together. It walks you through all of the steps you need to get this up and running.
If you are worried about the idea of mocks making it into a production environment for any reason, you can consider using a 3rd party solution like Charles Proxy.
Using the map local tool you can route calls from a specific endpoint to a local file on your machine. You can past plain text in your local file containing the response you want it to return. Per your example:
Your login hits endpoint yoursite.com/login
in Charles you using the map local tool you can route the calls hitting that endpoint to a file saved on your computer i.e mappedlocal.txt
mappedlocal.txt contains the following text
HTTP/1.1 404 Failed
When Charles is running and you hit this endpoint your response will come back with a 404 error.
You can also use another option in Charles called "map remote" and build an entire mock server which can handle calls and responses as you wish. This may not be exactly what you are looking for, but its an option that may help others, and its one I use myself.

Is it required that I use forge.request.ajax in stead of $.post?

First , I am not trying to do a cross site request.
I have created a small JS app and I can test in in a browser and it works fine i.
I see that my request from the app makes it to my server and I can confirm that my server responds with the data I expect. The problem is that the compiled app triggers the .fail in my javascript.
Is is REQUIRED that I use forge.request.ajax. instead of $.post? I see many posts saying I should but no one says I cannot use $.post .It makes debugging and development much easier being able to test all my JS in a browser before making the package.
Unfortunately I haven't found a way around it. You can use a promise, create a wrapper function/service, detect the platform that you are running on and call the corresponding function.

How to simulate external APIs?

We call many different external APIs in our system and now I'm looking for a system I can use to simulate those APIs so we can test ours in the Staging and Development environments?
Our application is written in Ruby on Rails 3.0 but since all the API calls to and from it are over HTTP there is no language dependency.
VCR will record the actual input from the webservice and then replay that feedback from then on.
To simulate it completely, you can use fakeweb. You'll record output to a file and have it sent back to your application.
This something called test mocking/stubbing and is a common practice. Basically you override the response code of the API call to return data w/o actually doing the HTTP request. Just search it for more details.

Resources