0
0
iOS Swiftmobile~20 mins

Test-driven development in Swift in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Simple Counter
A screen with a number display and a button to increase the count. The app is built using test-driven development (TDD) to ensure the counter logic works correctly.
Target UI
-------------------
| Simple Counter   |
-------------------
|                 |
|       0         |
|                 |
|   [Increment]   |
-------------------
Display a number starting at 0
Add a button labeled 'Increment'
When the button is tapped, increase the number by 1
Write a unit test to verify the increment logic before implementing it
Starter Code
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 40) {
            Text("\(count)")
                .font(.largeTitle)
                .accessibilityLabel("Current count")
            Button("Increment") {
                // TODO: Increase count by 1
            }
            .accessibilityLabel("Increment button")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

// TODO: Add Counter class and unit test in separate test target
Task 1
Task 2
Task 3
Solution
iOS Swift
import SwiftUI

class Counter: ObservableObject {
    @Published private(set) var count = 0

    func increment() {
        count += 1
    }
}

struct ContentView: View {
    @StateObject private var counter = Counter()

    var body: some View {
        VStack(spacing: 40) {
            Text("\(counter.count)")
                .font(.largeTitle)
                .accessibilityLabel("Current count")
            Button("Increment") {
                counter.increment()
            }
            .accessibilityLabel("Increment button")
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

// Unit Test (in test target)
import XCTest
@testable import YourAppModuleName

final class CounterTests: XCTestCase {
    func testIncrementIncreasesCount() {
        let counter = Counter()
        XCTAssertEqual(counter.count, 0)
        counter.increment()
        XCTAssertEqual(counter.count, 1)
    }
}

We created a Counter class that holds the count and a method increment() to increase it by 1. This class uses @Published so SwiftUI updates the UI when the count changes.

The ContentView uses @StateObject to keep the counter alive and updates the displayed number when increment() is called by tapping the button.

Before implementing the increment logic, we wrote a unit test testIncrementIncreasesCount to check that the count starts at 0 and increases to 1 after calling increment(). This follows test-driven development principles.

Final Result
Completed Screen
-------------------
| Simple Counter   |
-------------------
|                 |
|       1         |
|                 |
|   [Increment]   |
-------------------
User taps 'Increment' button
Number on screen increases by 1 each tap
Stretch Goal
Add a 'Reset' button that sets the count back to 0
💡 Hint
Add a reset() method in Counter class and a new Button in ContentView that calls it