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