0
0
iOS Swiftmobile~20 mins

Unit testing ViewModels in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: CounterViewModel Test
This screen tests a simple CounterViewModel that increments and resets a count. It ensures the ViewModel logic works correctly without UI.
Target UI
-------------------------
| CounterViewModel Test |
|-----------------------|
| [Test Increment]      |
| [Test Reset]          |
|-----------------------|
| Results:              |
|                       |
|                       |
-------------------------
Create a CounterViewModel class with a count property starting at 0
Add increment() method to increase count by 1
Add reset() method to set count back to 0
Write unit tests to verify increment() and reset() behavior
Display test results in console or test output
Starter Code
iOS Swift
import Foundation

class CounterViewModel {
    // TODO: Add count property
    
    // TODO: Add increment() method
    
    // TODO: Add reset() method
}

// TODO: Write unit tests for CounterViewModel
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
import Foundation
import XCTest

class CounterViewModel {
    private(set) var count: Int = 0
    
    func increment() {
        count += 1
    }
    
    func reset() {
        count = 0
    }
}

final class CounterViewModelTests: XCTestCase {
    var viewModel: CounterViewModel!
    
    override func setUp() {
        super.setUp()
        viewModel = CounterViewModel()
    }
    
    override func tearDown() {
        viewModel = nil
        super.tearDown()
    }
    
    func testIncrementIncreasesCount() {
        XCTAssertEqual(viewModel.count, 0, "Initial count should be 0")
        viewModel.increment()
        XCTAssertEqual(viewModel.count, 1, "Count should be 1 after one increment")
        viewModel.increment()
        XCTAssertEqual(viewModel.count, 2, "Count should be 2 after two increments")
    }
    
    func testResetSetsCountToZero() {
        viewModel.increment()
        XCTAssertEqual(viewModel.count, 1, "Count should be 1 before reset")
        viewModel.reset()
        XCTAssertEqual(viewModel.count, 0, "Count should be 0 after reset")
    }
}

We created a CounterViewModel class with a count property starting at 0. The increment() method adds 1 to count, and reset() sets it back to 0.

We then wrote unit tests using XCTest. The tests check that increment() correctly increases the count and that reset() sets it back to zero. This ensures the ViewModel logic works as expected without any UI.

Final Result
Completed Screen
-------------------------
| CounterViewModel Test |
|-----------------------|
| [Test Increment]      |
| [Test Reset]          |
|-----------------------|
| Results:              |
| All tests passed!     |
|                       |
-------------------------
Running tests shows 'All tests passed!' in the test console
If increment() or reset() fail, test output shows failure details
Stretch Goal
Add a test for multiple increments and verify count matches expected value
💡 Hint
Call increment() multiple times in a loop and check count equals the loop count