Skip to content

Instantly share code, notes, and snippets.

@rmirabelli
Created December 17, 2021 20:14
Show Gist options
  • Save rmirabelli/a76deb2e0ed14ee25df0feb0521f0a35 to your computer and use it in GitHub Desktop.
Save rmirabelli/a76deb2e0ed14ee25df0feb0521f0a35 to your computer and use it in GitHub Desktop.
Unit tests in Swift for async/await
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