0
0
iOS Swiftmobile~20 mins

Unit testing ViewModels in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewModel Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
How does ViewModel update UI state?
Consider a ViewModel that updates a published property to change the UI. What will the UI show after calling loadData() if the data fetch succeeds?
iOS Swift
class SampleViewModel: ObservableObject {
  @Published var message = ""
  func loadData() {
    message = "Loading..."
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      self.message = "Data loaded"
    }
  }
}

let vm = SampleViewModel()
vm.loadData()
// After 2 seconds, what is vm.message?
A"Loading..."
B""
C"Data loaded"
Dnil
Attempts:
2 left
💡 Hint
Think about asynchronous updates and when the message changes.
lifecycle
intermediate
1:30remaining
When is ViewModel deinitialized?
Given this ViewModel and usage, when will the deinit print statement run?
iOS Swift
class MyViewModel {
  deinit {
    print("ViewModel deinitialized")
  }
}

var vm: MyViewModel? = MyViewModel()
vm = nil
// When does "ViewModel deinitialized" print?
AImmediately after setting vm = nil
BNever, because vm is a strong reference
CAfter a delay of 5 seconds
DOnly when the app closes
Attempts:
2 left
💡 Hint
Think about reference counting and when objects are freed.
🔧 Debug
advanced
2:30remaining
Why does this ViewModel test fail?
This unit test expects the ViewModel's isLoading to be false after fetchData(). Why does it fail?
iOS Swift
class TestViewModel {
  var isLoading = false
  func fetchData() {
    isLoading = true
    DispatchQueue.global().async {
      sleep(1)
      self.isLoading = false
    }
  }
}

let vm = TestViewModel()
vm.fetchData()
assert(vm.isLoading == false)  // Fails here
ABecause isLoading is set to false asynchronously after the assert runs
BBecause isLoading is never set to true
CBecause sleep(1) blocks the main thread
DBecause assert syntax is incorrect
Attempts:
2 left
💡 Hint
Consider when the assert runs compared to the async update.
🧠 Conceptual
advanced
1:30remaining
What is the main benefit of unit testing ViewModels?
Why do developers write unit tests specifically for ViewModels in an iOS app?
ATo test UI animations and transitions
BTo verify business logic without UI dependencies
CTo check network connectivity directly
DTo improve app launch speed
Attempts:
2 left
💡 Hint
Think about separation of concerns in app architecture.
📝 Syntax
expert
2:00remaining
What error does this ViewModel test code produce?
What error occurs when running this Swift unit test code?
iOS Swift
class VM {
  var count = 0
  func increment() {
    count += 1
  }
}

func testIncrement() {
  let vm: VM? = VM()
  vm.increment()
  assert(vm?.count == 1)
}
testIncrement()
ACompile-time error: Missing return in function
BRuntime error: nil unwrapping
CTest passes with no error
DCompile-time error: Cannot call method on optional without unwrapping
Attempts:
2 left
💡 Hint
Look at how vm is declared and how increment() is called.