Recall & Review
beginner
What is the main purpose of unit testing a ViewModel in iOS development?
To verify that the ViewModel correctly processes data and business logic independently from the UI and external dependencies.
Click to reveal answer
beginner
Which Swift testing framework is commonly used for unit testing ViewModels?
XCTest is the standard framework used for unit testing in Swift, including ViewModels.
Click to reveal answer
intermediate
Why should dependencies of a ViewModel be mocked during unit testing?
Mocking dependencies isolates the ViewModel's logic, ensuring tests are fast, reliable, and only fail due to ViewModel issues.
Click to reveal answer
intermediate
What is a common pattern to test asynchronous code in ViewModels?
Using XCTestExpectation to wait for asynchronous operations to complete before asserting results.
Click to reveal answer
intermediate
How can you verify that a ViewModel updates its published properties correctly in SwiftUI?
By observing the published properties and asserting their values after triggering ViewModel actions in tests.
Click to reveal answer
What does mocking a dependency in a ViewModel test help achieve?
✗ Incorrect
Mocking isolates the ViewModel logic so tests focus only on the ViewModel's behavior.
Which XCTest feature helps test asynchronous ViewModel methods?
✗ Incorrect
XCTestExpectation waits for async code to finish before assertions.
What is a ViewModel's role in MVVM architecture?
✗ Incorrect
The ViewModel handles business logic and prepares data for the View.
When unit testing a ViewModel, what should you avoid?
✗ Incorrect
Unit tests for ViewModels should not test UI elements directly; that belongs to UI tests.
How do you check if a @Published property in a ViewModel changes as expected?
✗ Incorrect
Observing and asserting the property after actions confirms correct updates.
Explain how you would write a unit test for a ViewModel method that fetches data asynchronously.
Think about waiting for async calls and controlling dependencies.
You got /4 concepts.
Describe why mocking dependencies is important when unit testing ViewModels and how it improves test quality.
Consider how external factors can cause flaky tests.
You got /4 concepts.