0
0
iOS Swiftmobile~5 mins

Unit testing ViewModels in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIsolate the ViewModel logic from external factors
BMake the app run faster in production
CAutomatically generate UI components
DReplace the ViewModel with a simpler class
Which XCTest feature helps test asynchronous ViewModel methods?
AXCTAssertNil
BXCTAssertEqual
CXCTFail
DXCTestExpectation
What is a ViewModel's role in MVVM architecture?
AManage UI layout and appearance
BHandle business logic and data for the View
CStore user preferences only
DConnect directly to the database
When unit testing a ViewModel, what should you avoid?
AMocking network calls
BChecking property values
CTesting UI elements directly
DTesting business logic
How do you check if a @Published property in a ViewModel changes as expected?
AObserve the property and assert its new value after an action
BCall the property directly without triggering actions
CUse print statements in the ViewModel
DIgnore property changes in tests
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.