簡體   English   中英

IOS中的單元測試BLE應用程序(Swift)

[英]Unit testing BLE application in 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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM