Created
February 24, 2021 14:25
-
-
Save prokash-sarkar/cbd90ff1531049cf6ee08c207230e4e8 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 ( | |
private val dispatcher: CoroutineDispatcher | |
) : ViewModel() { | |
private var userData: Any? = null | |
fun getUserData(): Any? = userData | |
suspend fun saveSessionData() { | |
viewModelScope.launch(dispatcher) { | |
userData = "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.getUserData() | |
assertEquals("some_user_data", userData) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment