pdegand
10/25/2018, 9:11 AM@Test
fun mockSuspendUnitTest = runBlocking {
given(presenter.present()).will { throw Error() } // presenter.present() is a suspend fun
// when
viewModel = MyViewModel(presenter)
// then
viewModel.errorLiveData.observeForever(errorObserver)
then(errorObserver).should().onChanged("error")
}
with the ViewModel :
class MyViewModel(presenter: MyPresenter): ScopedViewModel() {
val errorLiveData = MutableLiveData<String>()
init {
load()
}
private fun load() {
launch {
try {
presenter.present()
} catch(e: Exception) {
errorLiveData.postValue("error")
}
}
}
}
But with 1.0.0-RC1, the presenter.present() call in the ViewModel is no longer mocked properly and my exception is never thrown.