اختبار التعليمات البرمجية غير المتزامنة
تحقق من تدفقات async/await بشكل موثوق
اختبار التعليمات البرمجية غير المتزامنة درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في SwiftUI Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Async Needs Special Care
Network calls and tasks finish later, not instantly. Your tests must wait for the result before asserting, or they check too early. ⏳
Mark the Test async
The simplest fix is an async test method. Swift lets you await directly, and the runner pauses until the work completes.
func testLoad() async {
}Await Your Method
Inside an async test you can use await just like real code. The line pauses until your ViewModel finishes loading.
await vm.load()Assert After Awaiting
Once the await returns, the data is ready. Now your assertion checks the final state, not an empty one. ✅
XCTAssertEqual(vm.items.count, 3)Throwing Async Tests
If the code can throw, mark the test throws too. Then try await lets errors bubble up and fail the test cleanly.
func testFetch() async throws {
try await vm.fetch()
}Avoid Real Networks
Hitting a live server makes tests slow and flaky. Inject a mock service that returns fixed data instead.
A Mock Service
A mock conforms to the same protocol but returns canned values. Your ViewModel cannot tell the difference, so the logic is tested honestly.
struct MockAPI: API {
func load() async -> [Item] { sample }
}Testing the Error Path
Make your mock throw on demand. Then you can verify the ViewModel sets an error state instead of crashing.
Expectations for Callbacks
For older callback APIs, use an XCTestExpectation. Fulfill it inside the callback, then wait so the test does not end early.
let exp = expectation(description: "done")Wait With a Timeout
Pair every expectation with a timeout. If the callback never fires, the test fails fast instead of hanging forever.
await fulfillment(of: [exp], timeout: 2)Prefer async Over Callbacks
When you can, write code with async/await. Tests stay short, linear, and far easier to read than nested callbacks.
Quick Check
How should you wait for async work in a modern XCTest?
Recap
You can now test async code: mark tests async, await results, mock the network, and use expectations for callbacks. Reliable every run. 🎉
الأسئلة الشائعة
هل درس «اختبار التعليمات البرمجية غير المتزامنة» مجاني؟
نعم — نص درس «اختبار التعليمات البرمجية غير المتزامنة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.
ماذا ستتعلم في «اختبار التعليمات البرمجية غير المتزامنة»؟
تحقق من تدفقات async/await بشكل موثوق تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟
لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «اختبار التعليمات البرمجية غير المتزامنة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟
نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اختبار ViewModels اختبارًا وحداتيًا
- اختبار التعليمات البرمجية غير المتزامنة
- اختبارات واجهة المستخدم باستخدام XCUITest
- الاختبار المدفوع باللقطات والمعاينات