RxJava 2 overriding IO scheduler in unit test

前端 未结 4 1987
你的背包
你的背包 2021-02-02 13:28

I\'m trying to test the following RxKotlin/RxJava 2 code:

validate(data)
    .subscribeOn(Schedulers.io())
    .observeO         


        
4条回答
  •  囚心锁ツ
    2021-02-02 14:10

    Figured it out! It had to do with the fact that in this code:

    validate(data)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap { ... }
    

    validate(data) was returning an Observable, which was emitting the following: emitter.onNext(null). Since RxJava 2 no longer accepts null values, flatMap was not getting called. I changed validate to return a Completable and updated the scheduler override to the following:

    RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
    

    Now the tests pass!

提交回复
热议问题