Created
February 24, 2021 14:27
-
-
Save prokash-sarkar/180aa0b2339a7629b0fbe46224d25063 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: 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