[英]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.