Load files in xcode unit tests

前端 未结 7 1902
失恋的感觉
失恋的感觉 2021-02-02 05:17

I have an Xcode 5 unit test project and some test xml files related to it. I\'ve tried a bunch of approaches but I can\'t seem to load the xml files.

I\'ve tried the fol

7条回答
  •  情歌与酒
    2021-02-02 05:41

    1. Get main bundle
    2. Use main bundle to get local mock data
    3. Load data from JSON

    let mainBundle = Bundle(identifier: "com.mainBundle.Identifier")

    if let path = mainBundle?.path(forResource: "mockData", ofType: "json") {
            do {
                let testData = try Data(contentsOf: URL(fileURLWithPath: path))
                networkTestSession.data = testData
            } catch {
                debugPrint("local json test data missing")
            }
        }
    

提交回复
热议问题