Mock API 请求 Xcode 7 Swift 自动化 UI 测试 [英] Mock API Requests Xcode 7 Swift Automated UI Testing

查看:57
本文介绍了Mock API 请求 Xcode 7 Swift 自动化 UI 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 2.0 中编写自动化 UI 测试时,有没有办法模拟请求.据我所知,UI 测试应该独立于其他功能.有没有办法模拟来自服务器请求的响应,以测试依赖于响应的 UI 的行为.例如,如果服务器关闭,UI 测试仍应运行.快速示例,对于登录,如果密码失败则进行模拟,然后 UI 应显示警报,但是,如果登录成功,则应显示下一页.

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.

推荐答案

在其当前的实现中,这不能通过 UI 测试直接实现.框架与代码直接相连的唯一接口是通过它的 启动参数/环境.

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.

您可以让应用在此上下文中查找特定键或值并切换某些功能.例如,如果设置了 MOCK_REQUESTS 键,则在您的网络层中注入一个 MockableHTTPClient 而不是真正的 HTTPClient.我写了关于设置参数,NSHipster 有一篇文章关于如何阅读它们.

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.

这是我整理的 关于为 UI 测试截取网络数据的教程.它会引导您完成启动和运行所需的所有步骤.

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.

这篇关于Mock API 请求 Xcode 7 Swift 自动化 UI 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆