Search code examples
androidunit-testingrx-javamvrx

How to skip network call with repeating events in test?


I'm not sure if I miss something in MvRx, MvRxTestRule or in RxJava.

It's common that in a ViewModel I want to test a network call and if it triggers another call. So when I have something like this:

fun fetchResult() {
 someRepository.fetchResult()
  .compose(schedulerProvider.getSchedulersForSingle())
  .execute {
    copy(
     ...
    )
  }
}

That works in Unit tests:

viewModel?.fetchResult()
viewModel?.someEvent()
withState(viewModel!!) {
    assert(it.someOtherRequest == Uninitialized)
}

However if the call is repeating:

fun fetchResult() {
  withState { state ->
    disposable = Observable.interval(0L, 6L, TimeUnit.SECONDS)
      .flatMapSingle {
        revolutRepository.fetchResult()
          .compose(schedulerProvider.getSchedulersForSingle())
      }
      .compose(schedulerProvider.getSchedulersForObservable())
      .execute {
        copy(
          ...
        )
      }
  }
}

I cannot get past viewModel?.fetchResult() in the test. How can I make it work?

MvRxTestRule() is in the companion object of the tests.


Solution

  • I moved the call with the interval to the repo and mocked that. That solved the problem.