如何在 Xcode 7 上的 UITest 中模拟数据? [英] How to mock data in UITest on Xcode 7?

查看:19
本文介绍了如何在 Xcode 7 上的 UITest 中模拟数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人尝试在新的 Xcode 7 UI 测试中包含模拟数据?

Someone has tried to include mock data with the new Xcode 7 UI tests?

  • 您是否使用过特定的框架?
  • 您是如何管理目标的?

推荐答案

我认为有很多方法可以解决这个问题 - 困难在于 Apple 有意将 UITests 设计为完全独立于被测应用程序运行.也就是说,您可以使用一些钩子来协调应用程序中的逻辑与测试中的逻辑,以提供模拟数据或以任何方式改变应用程序的行为.我发现最有用的两个是 launchEnvironmentlaunchArguments.

I think there are a lot of ways to approach this one - the difficulty is that Apple has intentionally designed UITests to run entirely separate from the app under test. That said, there are a few hooks you can use to coordinate logic in the app with logic in your tests to feed in mock data or alter the behavior of your app in any way. The two I have found most useful are launchEnvironment and launchArguments.

在您的测试中 - XCUIApplication().launchArguments 对应于您的应用代码中的 NSProcessInfo.processInfo().arguments

in your test - XCUIApplication().launchArguments corresponds to NSProcessInfo.processInfo().arguments in your app code

同样:XCUIApplication().launchEnvironment -> NSProcessInfo.processInfo().environment

launchEnvironment 是一个简单的字典,而启动参数是一个数组.在您的测试中,您可以在启动应用程序之前将您喜欢的任何值输入到这些参数中:

launchEnvironment is a straight forward dictionary whereas launch arguments is an array. In your test you can feed any values you like into either of these parameters before you launch the app:

let app = XCUIApplication()
app.launchEnvironment["-FakedFeedResponse"] = "success.json"
app.launch()

然后在您的应用程序逻辑中,您可以随意打开这些值.比如:

Then in your application logic you can switch on these values however you like. Something like:

func fetchFeed() -> JSON {
    if let fakedJSONFilename = NSProcessInfo.processInfo().environment["-FakedFeedResponse"] {
        let fakePayload = fakeDataFileNamed(fakedJSONFilename)
        return fakePayload
    } else {
       //Make network call and return a real JSON payload 
    }
}

使用此模式,您的伪造/模拟数据将需要作为应用目标成员包含在内的文件.

Using this pattern your faked/mock data will need to be files included as members of the app target.

这篇关于如何在 Xcode 7 上的 UITest 中模拟数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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