Mock API Requests Xcode 7 Swift Automated UI Testing

两盒软妹~` 提交于 2019-12-05 00:53:41

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!