Created
December 17, 2021 20:14
-
-
Save rmirabelli/a76deb2e0ed14ee25df0feb0521f0a35 to your computer and use it in GitHub Desktop.
Unit tests in Swift for async/await
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
import XCTest | |
@testable import AsyncAwaitModule | |
class AsyncAwaitModuleTests: XCTestCase { | |
// note the complexity in this unit test | |
func testClosure() throws { | |
let expectation = expectation(description: "posts fetch") | |
PostsService().fetchPosts { result in | |
let posts = try? result.get() | |
XCTAssert(!(posts?.isEmpty ?? true)) | |
expectation.fulfill() | |
} | |
wait(for: [expectation], timeout: 4.0) | |
} | |
// and then with async/await, it becomes effortless. | |
func testAsync() async throws { | |
let posts = try await PostsService().posts() | |
XCTAssert(!posts.isEmpty) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment