Last active
July 1, 2021 06:02
-
-
Save prokash-sarkar/5f88240d1c0859e48c8f6683fc0ef240 to your computer and use it in GitHub Desktop.
This file contains 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
class MainViewModel : ViewModel() { | |
private var userData: Any? = null | |
fun getUserData(): Any? = userData | |
suspend fun saveSessionData() { | |
viewModelScope.launch { | |
userData = "some_user_data" | |
} | |
} | |
} | |
@ExperimentalCoroutinesApi | |
class MainViewModelTest { | |
@ExperimentalCoroutinesApi | |
private val testDispatcher = TestCoroutineDispatcher() | |
@Before | |
fun setup() { | |
// 1 | |
Dispatchers.setMain(testDispatcher) | |
} | |
@After | |
fun tearDown() { | |
// 2 | |
Dispatchers.resetMain() | |
// 3 | |
testDispatcher.cleanupTestCoroutines() | |
} | |
@ExperimentalCoroutinesApi | |
@Test | |
fun testsSaveSessionData() = runBlockingTest { | |
val mainViewModel = MainViewModel() | |
mainViewModel.saveSessionData() | |
val userData = mainViewModel.getUserData() | |
assertEquals("some_user_data", userData) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment