Fixing unit test issue of RxJava Chain and Kotlin

Testing on RxJava or Kotlin itself at times post some hurdles. Doing both together without any experience would be a challenge. Hopefully this blog helps clear some issue.

Background

If you have read my blog below, it shares on using RxJava to manage Network State.

The single chain handle the entire network state flow. It’s an important part. So we would like to test every single scenarios of data fetched is handled with the right view response.

service.fetchResult()
.map { data -> UiStateModel.success(data) }
.onErrorReturn { exception -> UiStateModel.error(exception) }
.startWith(UiStateModel.loading())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
uiState ->
when {
uiState.isLoading() -> view.isLoading()
uiState.isError() -> view.isError(uiState.getErrorMessage())
uiState.isSuccess() -> view.isSuccess(uiState.getData())
uiState.isEmpty() -> view.isEmpty()
else -> IllegalArgumentException("Invalid Response")
}
})

--

--