0Pricing
SwiftUI Academy · レッスン

非同期コードのテスト

async/awaitのフローを確実に検証します。

「非同期コードのテスト」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Async Needs Special Care

Network calls and tasks finish later, not instantly. Your tests must wait for the result before asserting, or they check too early. ⏳

Mark the Test async

The simplest fix is an async test method. Swift lets you await directly, and the runner pauses until the work completes.

func testLoad() async {
}

Await Your Method

Inside an async test you can use await just like real code. The line pauses until your ViewModel finishes loading.

await vm.load()

Assert After Awaiting

Once the await returns, the data is ready. Now your assertion checks the final state, not an empty one. ✅

XCTAssertEqual(vm.items.count, 3)

Throwing Async Tests

If the code can throw, mark the test throws too. Then try await lets errors bubble up and fail the test cleanly.

func testFetch() async throws {
  try await vm.fetch()
}

Avoid Real Networks

Hitting a live server makes tests slow and flaky. Inject a mock service that returns fixed data instead.

A Mock Service

A mock conforms to the same protocol but returns canned values. Your ViewModel cannot tell the difference, so the logic is tested honestly.

struct MockAPI: API {
  func load() async -> [Item] { sample }
}

Testing the Error Path

Make your mock throw on demand. Then you can verify the ViewModel sets an error state instead of crashing.

Expectations for Callbacks

For older callback APIs, use an XCTestExpectation. Fulfill it inside the callback, then wait so the test does not end early.

let exp = expectation(description: "done")

Wait With a Timeout

Pair every expectation with a timeout. If the callback never fires, the test fails fast instead of hanging forever.

await fulfillment(of: [exp], timeout: 2)

Prefer async Over Callbacks

When you can, write code with async/await. Tests stay short, linear, and far easier to read than nested callbacks.

Quick Check

How should you wait for async work in a modern XCTest?

Recap

You can now test async code: mark tests async, await results, mock the network, and use expectations for callbacks. Reliable every run. 🎉

よくある質問

「非同期コードのテスト」レッスンは無料ですか?

はい。「非同期コードのテスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「非同期コードのテスト」で何を学びますか?

async/awaitのフローを確実に検証します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「非同期コードのテスト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ViewModelのユニットテスト
  2. 非同期コードのテスト
  3. XCUITestによるUIテスト
  4. スナップショットとプレビュー駆動テスト
← SwiftUI Academyに戻る