Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class MainViewModel(
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var _userData: MutableLiveData<Any> = MutableLiveData<Any>()
val userData: LiveData<Any> = _userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
_userData.value = "some_user_data"
}
}
}
@ExperimentalCoroutinesApi
class MainViewModelTest {
private val testDispatcher = TestCoroutineDispatcher()
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@Test
fun testsSaveSessionData() = runBlockingTest {
val mainViewModel = MainViewModel(testDispatcher)
mainViewModel.saveSessionData()
val userData = mainViewModel.userData.value
assertEquals("some_user_data", userData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment