2017-12-06 1 views
0

우리는 BLE 동글에 연결하고 모든 것이 BLE 연결을 통해 작동하는 앱을 개발했습니다.IOS (Swift)에서 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 단위 테스트를 작성하는 방법 및 단위 테스트 사이의 지연을 만드는 방법을 알고 에스?

답변

2

Objective-C의 네트워크 작동 테스트에 대한 기대치를 사용합니다.

예상치를 작성하고 테스트 케이스의 끝에서 충족 될 때까지 기다리십시오. 연결 알림 또는 기다려야 할 것이 있으면 fulfill()으로 전화하십시오. 대기는 시간 초과를 사용하며 알림이 절대로 나오지 않으면 (연결이 수행되지 않음) 테스트가 실패로 끝납니다. 스위프트 이미 애플의 웹 사이트 (here)의 샘플에서

:이 그냥 일이 될 수도

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

10 배, 내가 그것을 놓친 방법을 모르겠어요. – Erez

+1

Xcode 세계에서 자신의 길을 배울 때, 나는 종종 두 가지를 놓치기 쉽다는 것을 자주 발견했습니다. 몇몇 관련 측면이 문서의 특정 부분에 언급되어 있다면 그것은 더 좋을 것입니다 :-) –