การทดสอบหน่วยของ ViewModels
ทดสอบตรรกะทางธุรกิจแยกจากส่วนอื่น
การทดสอบหน่วยของ ViewModels เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 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. 🎉
คำถามที่พบบ่อย
บทเรียน “การทดสอบหน่วยของ ViewModels” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบหน่วยของ ViewModels” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบหน่วยของ ViewModels”
ทดสอบตรรกะทางธุรกิจแยกจากส่วนอื่น คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบหน่วยของ ViewModels” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยของ ViewModels
- การทดสอบโค้ดแบบอะซิงโครนัส
- การทดสอบส่วนติดต่อผู้ใช้ด้วย XCUITest
- การทดสอบด้วยภาพบันทึกและพรีวิว