Pick the fix
A view model refreshes every 30 seconds. How do you test it without waiting?
Hint 1 of 1
What does the code depend on?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Inject a Clock, use a test clock in tests, advance it and await the refresh
Correct. Time becomes a dependency the test controls.
Why not the others?
- B Call
Task.sleep(for: .seconds(31))in the test. That's a 31-second test, and still flaky. - C Run the test 10 times and accept it if most pass. Flaky tests hide real bugs.
- D Turn off parallel testing. It doesn't remove the dependency on real time.