RxJava 2 overriding IO scheduler in unit test

前端 未结 4 1999
你的背包
你的背包 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:25

    I suggest you take a different approach and add a layer of abstraction to your schedulers. This guy has a nice article about it.

    It would look something like this in Kotlin

    interface SchedulerProvider {
        fun ui(): Scheduler
        fun computation(): Scheduler
        fun trampoline(): Scheduler
        fun newThread(): Scheduler
        fun io(): Scheduler 
    }
    

    And then you override that with your own implementation of SchedulerProvider:

    class AppSchedulerProvider : SchedulerProvider {
        override fun ui(): Scheduler {
            return AndroidSchedulers.mainThread()
        }
    
        override fun computation(): Scheduler {
            return Schedulers.computation()
        }
    
        override fun trampoline(): Scheduler {
            return Schedulers.trampoline()
        }
    
        override fun newThread(): Scheduler {
            return Schedulers.newThread()
        }
    
        override fun io(): Scheduler {
            return Schedulers.io()
        }
    }
    

    And one for testing classes:

    class TestSchedulerProvider : SchedulerProvider {
        override fun ui(): Scheduler {
            return Schedulers.trampoline()
        }
    
        override fun computation(): Scheduler {
            return Schedulers.trampoline()
        }
    
        override fun trampoline(): Scheduler {
            return Schedulers.trampoline()
        }
    
        override fun newThread(): Scheduler {
            return Schedulers.trampoline()
        }
    
        override fun io(): Scheduler {
            return Schedulers.trampoline()
        }
    }
    

    Your code would look like this where you call RxJava:

    mCompositeDisposable.add(mDataManager.getQuote()
            .subscribeOn(mSchedulerProvider.io())
            .observeOn(mSchedulerProvider.ui())
            .subscribe(Consumer {
    ...
    

    And you'll just override your implementation of SchedulerProvider based on where you test it. Here's a sample project for reference, I am linking the test file that would use the testable-version of SchedulerProvider: https://github.com/Obaied/DingerQuotes/blob/master/app/src/test/java/com/obaied/dingerquotes/QuotePresenterTest.kt#L31

提交回复
热议问题