Pick the fix
How should you test func load() async throws on a @MainActor view model?
Hint 1 of 1
Tests can be async too.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A A @MainActor async test that calls try await viewModel.load() and then asserts
Correct. It awaits the real work, with no sleeps.
Why not the others?
- B Call load in a Task, sleep 2 seconds, then assert. Slow, and flaky under load.
- C Use DispatchSemaphore to block until it finishes. Blocking can deadlock with main-actor work.
- D Only test the synchronous parts. The async path is where bugs hide.