我一直在为我的应用程序编写测试。但是,当运行测试时,我的函数总是出现错误Stall on main thread

private func waitForElementToAppear(testCase: XCTestCase,
                                    element: XCUIElement,
                                    file: String = #file,
                                    line: UInt = #line) {

    let existsPredicate = NSPredicate(format: "exists == true")
    testCase.expectationForPredicate(existsPredicate,
                                     evaluatedWithObject: element, handler: nil)

    testCase.waitForExpectationsWithTimeout(5) { (error) -> Void in
        if (error != nil) {
            let message = "Failed to find \(element) after 5 seconds."
            testCase.recordFailureWithDescription(message,
                                                  inFile: file, atLine: line, expected: true)
        }
    }
}

我在测试/代码中多次使用此函数。我怎样才能把它转换成一个助手函数,所以我只需要编写一次这个函数。感谢您的事先帮助:)

最佳答案

使用分机更快捷:

extension XCTestCase {

    // your function here
}

因此,您可以在所有测试类中使用它,而不需要额外的类

关于swift - 如何将私有(private)函数转换为辅助函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41810608/

10-14 19:56
查看更多