2017-01-23 71 views
2

我一直在为我的应用程序编写测试。但是运行我的测试时,我不断收到错误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) 
     } 
    } 
} 

我用这个功能好几次在我的测试/代码。我怎样才能将它转换成辅助函数,所以我只需要编写一次这个函数。感谢您提前帮助:)

回答

4

它更SWIFTY使用扩展:

extension XCTestCase { 

    // your function here 
} 

所以,你可以在所有的测试类使用它无需额外类

+0

感谢您的回答!我已经将该函数移入扩展类 - 但是现在,当尝试使用该函数时,出现错误“使用未解析的标识符'waitForElement'”,例如'waitForElementToAppear(app.staticTexts [“添加评论”])' –

+0

删除'私人' – Alistra

+0

我的男人,谢谢G! –

1

您可以创建一个辅助类,使功能静态

class Helper { 
    static func waitForElementToAppear(testCase: XCTestCase, 
           element: XCUIElement, 
           file: String = #file, 
           line: UInt = #line) { 
     // do stuff 
    } 
} 

这样,只要您需要使用的功能,你可以调用于辅助功能。

+0

感谢您的回答,我相信使用扩展方法是我首选的使用方法:) –

相关问题