Unit Testing ViewModels
Test business logic in isolation.
Unit Testing ViewModels is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test the ViewModel?
Your ViewModel holds the logic, so it is the perfect place to test. You can check behavior without ever launching the full UI. 🧪
Logic Lives Apart from Views
Because a ViewModel is a plain class, you can create one in a test and call its methods directly. This keeps your tests fast and focused.
Meet XCTest
Apple's testing framework is XCTest. You import it and write test classes that Xcode runs for you with a single keystroke.
import XCTest
@testable import MyAppA Test Class
A XCTestCase subclass groups related tests. Every test method name must start with the word test so Xcode discovers it automatically.
final class CounterVMTests: XCTestCase {
}Arrange, Act, Assert
A clean test follows three steps: arrange the objects, act by calling a method, then assert the result is what you expected.
Create the ViewModel
Start by making a fresh instance of the ViewModel inside your test. A new object per test keeps each one independent and reliable.
let vm = CounterViewModel()Call a Method
Now act on it. Call the method you want to verify, just like your view would when a button is tapped. ➡️
vm.increment()Assert with XCTAssertEqual
Use XCTAssertEqual to confirm a value matches what you expect. If it differs, the test fails with a clear message.
XCTAssertEqual(vm.count, 1)Testing Booleans
For true or false results, reach for XCTAssertTrue and XCTAssertFalse. They read clearly and document your intent.
XCTAssertTrue(vm.isEmpty)Test Edge Cases
Good tests cover more than the happy path. Check empty input, limits, and unexpected values so bugs surface before your users do.
Run with One Shortcut
Press Command-U in Xcode to run every test. Green checkmarks mean your logic still works after each change you make.
Quick Check
Which assertion confirms two values are equal?
Recap
You learned to test a ViewModel in isolation: create it, call a method, then assert the result with XCTest. Fast, focused, dependable. 🎉
Frequently asked questions
Is the “Unit Testing ViewModels” lesson free?
Yes — the full text of “Unit Testing ViewModels” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unit Testing ViewModels”?
Test business logic in isolation. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unit Testing ViewModels” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing ViewModels
- Testing Async Code
- UI Tests with XCUITest
- Snapshot & Preview-Driven Testing