Last active
August 14, 2020 08:13
-
-
Save luckyhandler/c0d90fb41473f504ee92b46ca8aa685b to your computer and use it in GitHub Desktop.
Test Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testingcoroutines | |
import kotlinx.coroutines.CoroutineDispatcher | |
import kotlinx.coroutines.Dispatchers | |
interface DispatchersProvider { | |
fun main(): CoroutineDispatcher = Dispatchers.Main | |
fun default(): CoroutineDispatcher = Dispatchers.Default | |
fun io(): CoroutineDispatcher = Dispatchers.IO | |
fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined | |
} | |
class DefaultDispatchersProvider : DispatchersProvider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testingcoroutines | |
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | |
import androidx.lifecycle.asFlow | |
import kotlinx.coroutines.ExperimentalCoroutinesApi | |
import kotlinx.coroutines.ObsoleteCoroutinesApi | |
import kotlinx.coroutines.flow.take | |
import kotlinx.coroutines.flow.toList | |
import org.junit.Before | |
import org.junit.Rule | |
import org.junit.Test | |
@ExperimentalCoroutinesApi | |
@ObsoleteCoroutinesApi | |
class UiStateTest { | |
@get:Rule | |
val instantExecutorRule = InstantTaskExecutorRule() | |
@get:Rule | |
val testCoroutineRule = TestCoroutineRule() | |
private lateinit var viewModel: ViewModel | |
private lateinit var expectedResult: List<NachrichtDto> | |
@Before | |
fun setup() = testCoroutineRule.runBlocking { | |
val dataSource = DataSource(dispatchersProvider = testCoroutineRule.testDispatcherProvider) | |
val repository = Repository(dataSource = dataSource) | |
viewModel = ViewModel( | |
dispatcher = testCoroutineRule.testDispatcherProvider.main(), | |
repository = repository | |
) | |
expectedResult = (repository.getNachrichten() as NetworkResult.Success).data | |
} | |
@Test | |
fun `ui states are emitted in the correct order`() = testCoroutineRule.runBlocking { | |
viewModel.getData() | |
val states = viewModel.stateLiveData.asFlow().take(2).toList() | |
assert(states.size == 2) | |
assert(states[0] == UiState.Loading) | |
assert(states[0] == UiState.Success(expectedResult)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testingcoroutines | |
sealed class UiState { | |
object Loading: UiState() | |
data class Success(val data: List<NachrichtDto>): UiState() | |
object Error: UiState() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testingcoroutines | |
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import kotlinx.coroutines.CoroutineDispatcher | |
import kotlinx.coroutines.launch | |
class ViewModel( | |
private val dispatcher: CoroutineDispatcher = DefaultDispatchersProvider().main(), | |
private val repository: Repository | |
) : ViewModel() { | |
val stateLiveData: LiveData<UiState> | |
get() = _stateLiveData | |
private val _stateLiveData = MutableLiveData<UiState>() | |
fun getData() { | |
viewModelScope.launch(dispatcher) { | |
_stateLiveData.value = UiState.Loading | |
when (val result = repository.getNachrichten()) { | |
is NetworkResult.Success -> _stateLiveData.value = UiState.Success(data = result.data) | |
is NetworkResult.Error -> _stateLiveData.value = UiState.Error | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment