ViewModels ve Flow'ları Test Etme
Mantığı test dağıtıcılarıyla birim testi yapın.
ViewModels ve Flow'ları Test Etme, CoddyKit'te ücretsiz bir Jetpack Compose Academy dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Jetpack Compose Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Test Logic Without the UI
A ViewModel holds your screen logic, so you can test it directly with plain JUnit. No emulator or rendering needed, which makes these tests fast. ⚡
Arrange, Act, Assert
ViewModel tests follow a simple rhythm: create the ViewModel, call a function, then assert the new state. Keep each test focused on one behavior.
val vm = CounterViewModel()
vm.increment()
assertEquals(1, vm.count)The Coroutine Challenge
Many ViewModels launch coroutines, which run on Dispatchers.Main. In tests there is no main looper, so you must swap in a test dispatcher instead.
StandardTestDispatcher
A StandardTestDispatcher gives you full control over coroutine timing. Tasks queue up and run only when you advance the virtual clock.
val dispatcher = StandardTestDispatcher()Replace the Main Dispatcher
Use Dispatchers.setMain before each test to inject your test dispatcher, then call resetMain afterward to leave global state clean.
Dispatchers.setMain(dispatcher)runTest
Wrap suspending test bodies in runTest. It provides a coroutine scope with a virtual clock so delays resolve instantly, not in real seconds.
@Test fun loads() = runTest {
// suspend calls run here
}Quick Check
Your ViewModel launches coroutines on the main dispatcher. What lets a unit test control their execution?
Advancing the Clock
With a StandardTestDispatcher, queued coroutines wait. Call advanceUntilIdle inside runTest to run them all, then check the final state.
advanceUntilIdle()
assertEquals(Loaded, vm.uiState.value)Testing a StateFlow
ViewModels often expose a StateFlow. Its current snapshot lives in .value, so you can read and assert it directly after advancing the clock.
assertEquals(0, vm.count.value)Collecting Flow Emissions
To capture a sequence of emissions, the Turbine library makes Flow testing easy with awaitItem, asserting each value as it arrives.
vm.events.test {
assertEquals(Saved, awaitItem())
}Fake Your Dependencies
Inject a fake repository that returns canned data. This keeps tests fast and lets you simulate success and error paths without a real network.
val vm = NewsViewModel(FakeRepo())Clean Teardown
Always pair setMain with resetMain in your teardown. Leaving a test dispatcher installed can leak into and break other tests in the suite.
@After fun tearDown() {
Dispatchers.resetMain()
}Recap: ViewModels & Flows
Test logic without UI by swapping in a test dispatcher, running code in runTest, and asserting StateFlow values. Fast, focused, reliable tests. 🏁
Sıkça Sorulan Sorular
“ViewModels ve Flow'ları Test Etme” dersi ücretsiz mi?
Evet — “ViewModels ve Flow'ları Test Etme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Jetpack Compose Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Jetpack Compose Academy kursu toplamda 4 dersten oluşur.
“ViewModels ve Flow'ları Test Etme” dersinde ne öğreneceğim?
Mantığı test dağıtıcılarıyla birim testi yapın. Jetpack Compose Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Jetpack Compose Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Jetpack Compose Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“ViewModels ve Flow'ları Test Etme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Jetpack Compose Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Jetpack Compose Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- createComposeRule ve Bulucular
- Doğrulamalar ve Eylem Gerçekleştirme
- Anlambilim ve Test Etiketleri
- ViewModels ve Flow'ları Test Etme