Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Last active November 28, 2023 15:47
Show Gist options
  • Save lucianoschillagi/b2923bb937dd490e90094cec0fb2c775 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/b2923bb937dd490e90094cec0fb2c775 to your computer and use it in GitHub Desktop.
Asynchronous Tests
import XCTest
final class AsyncTests: XCTestCase {
func testDownloadWebDataWithConcurrency() async {
// Create a URL for a webpage to download.
let url = URL(string: "https://apple.com")! // test passed! :)
// let url = URL(string: "https://applexfsda.com")! // test don't passed :(
// 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.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment