Skip to content

Instantly share code, notes, and snippets.

@zivkesten
Created July 24, 2020 06:08
Show Gist options
  • Save zivkesten/509f724e8646cf416f8cb8281e8559e3 to your computer and use it in GitHub Desktop.
Save zivkesten/509f724e8646cf416f8cb8281e8559e3 to your computer and use it in GitHub Desktop.
sealed class Lce<T> {
class Loading<T> : Lce<T>()
data class Content<T>(val packet: T) : Lce<T>()
data class Error<T>(val packet: T) : Lce<T>()
}
//Test code
@Test
fun eventToResult_eventIsScreeLoad_resultIsLoadingThenContentOfResults() {
//Given
val testObserverState = searchViewModel.viewState.test()
val spiedSearchViewModel = Mockito.spy(searchViewModel)
//When
spiedSearchViewModel.onEvent(Event.ScreenLoad)
//Then
verify(spiedSearchViewModel, times(1)).resultToViewState(Lce.Loading()) //Not the same Lce.Loading() object as in VM
verify(spiedSearchViewModel, times(1)).resultToViewState(Lce.Content(Result.SearchResults(mockResults)))
}
/// ViewModel code
override fun eventToResult(event: Event) {
when (event) {
is Event.ScreenLoad -> { onScreenLoad() }
}
}
private fun onScreenLoad() {
resultToViewState(Lce.Loading()) //Not the same Lce.Loading() object as in test
viewModelScope.launch {
val podcastsResponse = repository.getPodcastsASync()
resultToViewState(Lce.Content(Result.SearchResults(podcastsResponse.podcasts)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment