TrendingViewModel
class TrendingViewModel(
val trendingRepository: TrendingRepository
) : ViewModel() {
fun fetchTrendingRepos() {
if (isConnected) {
compositeDisposable.add(
trendingRepository.getTrendingRepos().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
run {
saveDataToDb(response)
}
},
{ error ->
}
)
)
}
private fun saveDataToDb(response: List<TrendingRepo>) {
trendingRepository.insertTrendingUsers(response)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(object : DisposableCompletableObserver() {
override fun onComplete() {
Log.d("Status", "Save Success")
}
override fun onError(e: Throwable) {
Log.d("Status", "error ${e.localizedMessage}")
}
})
}
TrendingViewModelTest
@RunWith(JUnit4::class)
class TrendingViewModelTest {
@Test
fun testWithNetwork() {
trendingViewModel.isConnected = true
Mockito.`when`(trendingRepository.getTrendingRepos()).thenReturn(
Single.just(list) )
trendingViewModel.fetchTrendingRepos()
// trendingRepository.insertTrendingUsers(list).blockingAwait()
verify(trendingRepository, times(1)).getTrendingRepos()
}