Pick the fix
How do you unit-test a view model that loads data without hitting the network?
Hint 1 of 1
Where is the seam?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Inject an HTTPClient protocol and pass a stub that returns canned data in tests
Correct. Tests are fast, deterministic and offline.
Why not the others?
- B Call a staging server from the unit tests. Tests become slow and flaky, and fail when staging is down.
- C Swizzle URLSession methods at runtime. It's fragile and relies on Objective-C runtime hacks.
- D Add a static
isTestingflag to a network singleton. Production code fills up with test branches.