Component - Unit testing ViewModels
This UI component shows a simple ViewModel with a counter and a button to increment it. Unit testing ensures the ViewModel logic works correctly without UI interaction.
This UI component shows a simple ViewModel with a counter and a button to increment it. Unit testing ensures the ViewModel logic works correctly without UI interaction.
ViewModelTestView ├── Text (shows counter value) └── Button (increments counter)
result after the test runs?
class CounterViewModel {
var count = 0
func increment() { count += 1 }
}
func testIncrement() {
let vm = CounterViewModel()
vm.increment()
let result = vm.count
}class MyViewModel {
var value = 0
func updateValue() { value = 10 }
}
func testUpdateValue() {
let vm = MyViewModel()
vm.updateValue()
XCTAssertEqual(vm.value, 0)
}userName property. Which approach correctly tests this behavior using XCTest?Pass the test and this pattern moves unknown