How to mock data in UITest on Xcode 7?

╄→гoц情女王★ 提交于 2019-12-18 11:31:58

问题


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

  • Have you used an specific framework?
  • How have you managed targets?

回答1:


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.

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

likewise: XCUIApplication().launchEnvironment -> NSProcessInfo.processInfo().environment

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.



来源:https://stackoverflow.com/questions/31403684/how-to-mock-data-in-uitest-on-xcode-7

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