Skip to content

Instantly share code, notes, and snippets.

@prokash-sarkar
Last active September 16, 2021 17:01
Show Gist options
  • Save prokash-sarkar/cf8a4c6be680d482aec6bfe5824483ab to your computer and use it in GitHub Desktop.
Save prokash-sarkar/cf8a4c6be680d482aec6bfe5824483ab to your computer and use it in GitHub Desktop.
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(Dispatchers.IO) {
userData = "some_user_data"
}
}
}
@ExperimentalCoroutinesApi
class MainViewModelTest {
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
@ExperimentalCoroutinesApi
@Test
fun testsSaveSessionData() = runBlockingTest {
val mainViewModel = MainViewModel()
mainViewModel.saveSessionData()
val userData = mainViewModel.getUserData()
assertEquals("some_user_data", userData)
}
}
@venusiza
Copy link

Would you please share the fragment class for this ViewModel? How dispatcher initialized?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment