King of Stock
Alamofire 사용하여 요청한 응답 결과를 기준으로 테스트 하기 본문
Alamofire를 사용하여 API를 만들어 놓고 테스트를 하는 소스 공유합니다.
비동기 요청이기 때문에 응답을 기다리는 형태가 되어야하고 애플에서 제공되는 클래스와 함수를 이용합니다.
아래는 소스...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testAlamofireWithWaitingResponse() { | |
let e = expectation(description: "Alamofire") //비동기 테스트를 위한 인스턴스 생성 | |
let uuidSite = "https://www.uuidgenerator.net/api/version4/5" | |
var responseString = "" | |
Alamofire.request(uuidSite).responseString { (response) in | |
if let responseBody = response.result.value { | |
responseString = responseBody | |
debugPrint("받았어요. \(responseString)") | |
} else { | |
if let error = response.error { | |
print("오류: \(error)") | |
} | |
} | |
e.fulfill() // 다 차길 기다리는 부분을 표시한다. 위에 있는 구문을 기다린다는 의미로 봐도 될듯 | |
} | |
waitForExpectations(timeout: 10, handler: nil)//결과를 10초 동안 기다린다 | |
XCTAssertNotNil(responseString) | |
XCTAssertTrue(responseString.count > 0) | |
} |
도움이 되길...
'IT 기술' 카테고리의 다른 글
swift 접근 제한자 (0) | 2018.04.09 |
---|---|
Firebase 로그 Off (0) | 2018.04.05 |
UITableView cellForRow(at:) (0) | 2018.04.04 |
UITableView tableView.endUpdates() 오류 발생시 (0) | 2018.04.02 |
xcode simulator 삭제 (0) | 2018.04.02 |
Comments