iOS(SWIFT)中的单元测试BLE应用程序



我们已经开发了一个连接到ble加密狗的应用

现在,我们决定添加单元测试(是的,我知道做TDD而不是这样要好得多,但这是情况)

在应用程序中,一切都在起作用,但是当我试图开发单元测试时,我无法通过连接阶段(GAT)我无法完成连接,无论如何,测试都可以通过一个在另一个之后,不要停止等待连接发生,身份验证,什么也没有)

func testConnect() {
    if viewController == nil {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
        if let vc = viewController {
            _ = vc.view
        }
    }
    viewController?.connectBluetooth();
}

func testAuthenticateByPin() {
    delay(5) {
        var error: NSError? = nil
        self.datConnection?.connect("ABCDEFG", withError: &error)
        XCTAssertNotNil(error, "Connect Error: (String(describing: error))")
        print("Connection: (String(describing: error))")
        self.datConnection?.authenticate(byPIN: "AD$FGR#", withError: &error)
        XCTAssertNotNil(error, "Error: (String(describing: error))")
        print("Auth: (String(describing: error))")
    }
}

func delay(_ delay:Double, closure:@escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

有人知道如何创建BLE单元测试以及如何在单元测试之间创建延迟?

我在Objective-C中使用我的网络操作测试的期望。

您会创建一个期望,在测试案例的结尾处,等待它得到实现。当您获得连接通知或任何需要等待的内容时,请致电fulfill()。等待使用超时,如果通知永远不会出现(连接永远不会发生),则测试将在未满足的期望下失败。

来自Apple网站的样本(此处),该样本已经在Swift中:

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)
}

最新更新