// cc. apple developer swift documentation example code
func testDownloadWebDataWithConcurrency() async throws {
// Create a URL for a webpage to download.
let url = URL(string: "https://apple.com")!
// Use an asynchronous function to download the webpage.
let dataAndResponse: (data: Data, response: URLResponse) = try await URLSession.shared.data(from: url, delegate: nil)
// Assert that the actual response matches the expected response.
let httpResponse = try XCTUnwrap(dataAndResponse.response as? HTTPURLResponse, "Expected an HTTPURLResponse.")
XCTAssertEqual(httpResponse.statusCode, 200, "Expected a 200 OK response.")
}func test_NetworkManager_FetchSuccess() {
// given
correctDrwNum = 1125
expectation = .init(description: "정확한 회차 정보에 대한 비동기 테스트")
// when
sut.fetchLotto(correctDrwNum) { [weak self] lotto, error in
guard let self else { return }
guard let lotto else {
XCTAssertNil(lotto, "로또 정보가 없으면 nil 입니다.")
expectation.fulfill()
return
}
// then
XCTAssertTrue(lotto.bnusNo >= 1, "정확한 회차를 반영하면 보너스 정보가 1~45 사의 숫자로 반환된다.")
XCTAssertGreaterThanOrEqual(lotto.bnusNo, 1)
// then
XCTAssertTrue(lotto.bnusNo <= 45, "정확한 회차를 반영하면 보너스 정보가 1~45 사의 숫자로 반환된다.")
XCTAssertLessThanOrEqual(lotto.bnusNo, 45)
// end asynchronous
expectation.fulfill()
}
wait(for: [expectation], timeout: 2.0)
}