简体   繁体   English

IOS中的单元测试BLE应用程序(Swift)

[英]Unit testing BLE application in IOS (Swift)

We've developed an app that connects to a BLE dongle and everything is working thru the BLE connection. 我们开发了一个连接到BLE加密狗的应用程序,一切都通过BLE连接工作。

Now we decided to add Unit testing ( And Yes, i know it is much better to do TDD and not this way but this is the situation) 现在我们决定添加单元测试(并且是的,我知道做TDD要好得多,而不是这种方式,但这是情况)

In the app everything is working, but when i'm trying to develop the unit tests i can't go pass the connection phase (GAT) i'm not getting the connection working an in any case the tests go thru one after the other and don't stop to wait for the connection to happen and authentication and nothing) 在应用程序中一切正常,但是当我正在尝试开发单元测试时,我无法通过连接阶段(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)
}

Any one knows how to create a BLE unit test and how to create a Delay between unit tests? 任何人都知道如何创建BLE单元测试以及如何在单元测试之间创建延迟?

I use expectations for my network operation tests in Objective-C. 我在Objective-C中使用了对网络操作测试的期望。

You create an expectation and at the end of the test case, wait for it to become fulfilled. 您创建了一个期望,并在测试用例结束时,等待它完成。 When you get the connection notification or whatever you have to wait for, call fulfill() . 当您收到连接通知或您必须等待的任何内容时,请致电fulfill() The wait uses a timeout and if the notification never comes (connection never takes place), the test will fail with the non-fulfilled expectation. 等待使用超时,如果通知永远不会发生(连接永远不会发生),则测试将以未满足的期望失败。

From a sample from Apple's website ( here ) which is already in Swift: 来自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