我正在使用XCTest框架编写IOS的测试用例。一旦网络通话结束,我们如何等待网络通话并开始执行。
目前,我正在使用sleep()等待。但是,这并不是做到这一点的最佳方法。
那么,还有其他方法可以做到吗?

最佳答案

阅读文档here

func testDownloadWebData() {
    // Create an expectation for a background download task.
    let expectation = XCTestExpectation(description: "Download apple.com home page")

    // Create a URL for a web page to be downloaded.
    let url = URL(string: "https://apple.com")!

    // Create a background task to download the web page.
    let dataTask = URLSession.shared.dataTask(with: url) { (data, _, _) in

        // Make sure we downloaded some data.
        XCTAssertNotNil(data, "No data was downloaded.")

        // Fulfill the expectation to indicate that the background task has finished successfully.
        expectation.fulfill()

    }

    // Start the download task.
    dataTask.resume()

    // Wait until the expectation is fulfilled, with a timeout of 10 seconds.
    wait(for: [expectation], timeout: 10.0)
}

关于ios - iOS UI测试-等待网络通话,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48903660/

10-10 02:27