Created
July 24, 2020 06:08
-
-
Save zivkesten/509f724e8646cf416f8cb8281e8559e3 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
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