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

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

问题描述

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

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


  • 您是否使用过特定的框架?

  • 你是如何管理目标的?

推荐答案

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

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是一个直接字典,而launch参数是一个数组。在测试中,您可以在启动应用程序之前将任何您喜欢的值提供给这些参数:

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天全站免登陆