اختبار ViewModels اختبارًا وحداتيًا
اختبر منطق الأعمال بمعزل عن بقية المكونات
اختبار ViewModels اختبارًا وحداتيًا درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🎉
تعلم Swift مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «اختبار ViewModels اختبارًا وحداتيًا» مجاني؟
نعم — نص درس «اختبار ViewModels اختبارًا وحداتيًا» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «اختبار ViewModels اختبارًا وحداتيًا»؟
اختبر منطق الأعمال بمعزل عن بقية المكونات تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «اختبار ViewModels اختبارًا وحداتيًا»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اختبار ViewModels اختبارًا وحداتيًا
- اختبار التعليمات البرمجية غير المتزامنة
- اختبارات واجهة المستخدم باستخدام XCUITest
- الاختبار المدفوع باللقطات والمعاينات