Skip to content

Instantly share code, notes, and snippets.

@prokash-sarkar
Created February 24, 2021 14:27
Show Gist options
  • Save prokash-sarkar/d0bcbb9d4e4040b1ffd1ff188c1e40df to your computer and use it in GitHub Desktop.
Save prokash-sarkar/d0bcbb9d4e4040b1ffd1ff188c1e40df to your computer and use it in GitHub Desktop.
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()
@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