Erez
Erez

Reputation: 1953

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.

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)

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)

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?

Upvotes: 1

Views: 887

Answers (1)

Rainer Schwarze
Rainer Schwarze

Reputation: 4745

I use expectations for my network operation tests in 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(). 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:

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

Upvotes: 3

Related Questions