محاكاة Ktor Client والمستودعات
استخدم MockEngine والمحاكيات لاختبارات حتمية النتائج.
محاكاة Ktor Client والمستودعات درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Tests Should Not Hit the Network
Real HTTP calls are slow and flaky in tests. Instead you fake the response so each run is fast and deterministic. 🎯
Ktor Ships a MockEngine
Ktor includes a built-in MockEngine that returns canned responses instead of making real requests.
Build a Mock Client
Construct an HttpClient on top of MockEngine and decide exactly what bytes and status it replies with.
val client = HttpClient(MockEngine) {
engine { addHandler { respondOk("[]") } }
}Return Custom JSON
Inside the handler, call respond with the JSON body and headers your code expects to parse.
respond(
content = "{\"name\":\"Sam\"}",
headers = headersOf(HttpHeaders.ContentType, "application/json")
)Simulate Errors
Have the handler reply with a status like 500 to verify your error handling without a real server.
respond("oops", HttpStatusCode.InternalServerError)Inject the Client
Your repository should accept its HttpClient as a parameter, so tests pass the mock and production passes the real one.
class UserRepo(private val client: HttpClient)Why a Fake Repository
To test a ViewModel you do not even need Ktor: pass a fake repository that returns fixed data.
Fake an Interface
Because your repo hides behind an interface, a fake is just a small class implementing it with canned results.
class FakeRepo : UserRepo {
override suspend fun load(id: Int) = User("Sam")
}Control Failure Paths
A fake can also throw on demand, letting you test how callers react to a failing data layer.
override suspend fun load(id: Int): User =
throw IOException("offline")Fakes vs Mocks
A hand-written fake is simple and multiplatform-safe, while mocking libraries often lack full Kotlin/Native support.
Deterministic by Design
With MockEngine and fakes, every test controls its inputs, so results never depend on a network or shared state. ✅
Quick Check
Pick the right way to fake HTTP.
Recap
Swap real HTTP for MockEngine and inject fake repositories so tests stay fast, isolated, and predictable.
الأسئلة الشائعة
هل درس «محاكاة Ktor Client والمستودعات» مجاني؟
نعم — نص درس «محاكاة Ktor Client والمستودعات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «محاكاة Ktor Client والمستودعات»؟
استخدم MockEngine والمحاكيات لاختبارات حتمية النتائج. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «محاكاة Ktor Client والمستودعات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- kotlin.test في commonTest
- اختبار الدوال المعلّقة وFlows
- محاكاة Ktor Client والمستودعات
- تشغيل الاختبارات على أهداف Android وiOS