0Pricing
SwiftUI Academy · Pelajaran

Menguji Kode Asinkron

Verifikasi alur async/await dengan andal.

Menguji Kode Asinkron adalah pelajaran SwiftUI Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar SwiftUI Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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. 🎉

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Menguji Kode Asinkron” gratis?

Ya — teks lengkap “Menguji Kode Asinkron” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus SwiftUI Academy, upgrade ke CoddyKit PRO. Kursus SwiftUI Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Menguji Kode Asinkron”?

Verifikasi alur async/await dengan andal. Kamu berlatih SwiftUI Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai SwiftUI Academy?

Tidak diperlukan pengalaman sebelumnya. SwiftUI Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Menguji Kode Asinkron” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran SwiftUI Academy ini?

Ya. Setiap pelajaran SwiftUI Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Menguji ViewModels secara Unit
  2. Menguji Kode Asinkron
  3. Pengujian UI dengan XCUITest
  4. Pengujian Berbasis Snapshot & Preview
← Kembali ke SwiftUI Academy