ทดสอบฟังก์ชัน suspend และโฟลว์
ใช้ runTest และ Turbine เพื่อตรวจสอบพฤติกรรมแบบอะซิงโครนัส
ทดสอบฟังก์ชัน suspend และโฟลว์ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Async Needs Special Tests
Suspend functions and Flows do not finish instantly, so you need tools that wait for async work before asserting. ⏳
Meet runTest
Wrap async assertions in runTest from the coroutines-test library; it provides a scope and skips virtual delays automatically.
@Test
fun loadsUser() = runTest {
val u = repo.load(1)
assertEquals("Sam", u.name)
}Add the Test Helper
Pull kotlinx-coroutines-test into commonTest so runTest and friends are available on every target.
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")Awaiting a suspend Result
Inside runTest you simply call the suspend function directly and assert on whatever it returns.
val total = calc.fetchTotal()
assertEquals(42, total)Time Skips for Free
runTest uses virtual time, so a delay of seconds resolves immediately and your tests stay fast.
Testing a Flow by Hand
You can collect a finite Flow into a list with toList and assert on the emitted values.
val values = numbers().toList()
assertEquals(listOf(1, 2, 3), values)Why Turbine Helps
For ongoing streams, the Turbine library lets you await items one at a time without manual collection plumbing.
Add Turbine
Drop Turbine into commonTest; it is a tiny multiplatform helper built just for testing Flows.
implementation("app.cash.turbine:turbine:1.1.0")Await Items with test
Turbine wraps a Flow in a test block where awaitItem returns each emission in order.
flow.test {
assertEquals(1, awaitItem())
awaitComplete()
}Finish the Stream Cleanly
End every Turbine block with awaitComplete or cancel, or the test fails for unconsumed events.
Assert StateFlow Updates
Turbine shines on StateFlow: await the initial value, trigger an action, then await the next emitted state.
Quick Check
Pick the right async test wrapper.
Recap
Use runTest for suspend code and Turbine's test block to await Flow emissions, all from commonTest.
คำถามที่พบบ่อย
บทเรียน “ทดสอบฟังก์ชัน suspend และโฟลว์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ทดสอบฟังก์ชัน suspend และโฟลว์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทดสอบฟังก์ชัน suspend และโฟลว์”
ใช้ runTest และ Turbine เพื่อตรวจสอบพฤติกรรมแบบอะซิงโครนัส คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ทดสอบฟังก์ชัน suspend และโฟลว์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- kotlin.test ใน commonTest
- ทดสอบฟังก์ชัน suspend และโฟลว์
- จำลองไคลเอนต์และคลังของ Ktor
- เรียกใช้การทดสอบบนเป้าหมาย Android และ iOS