0Pricing
SwiftUI Academy · 课时

对 ViewModel 进行单元测试

隔离测试业务逻辑。

对 ViewModel 进行单元测试 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 MyApp

A 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. 🎉

常见问题解答

「对 ViewModel 进行单元测试」课时是免费的吗?

是的 — 「对 ViewModel 进行单元测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「对 ViewModel 进行单元测试」这节课中我会学到什么?

隔离测试业务逻辑。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「对 ViewModel 进行单元测试」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 对 ViewModel 进行单元测试
  2. 测试异步代码
  3. 使用 XCUITest 进行用户界面测试
  4. 快照与预览驱动的测试
← 返回 SwiftUI Academy