IdlingResource Espresso with RxJava

后端 未结 3 1296
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 09:21

I recently converted my application from using async tasks to rxjava. Now, my espresso tests no longer wait for my data calls to complete due to espresso not having an idling r

3条回答
  •  误落风尘
    2021-02-04 10:04

    Wrote a little integration piece between RxJava Plugins and Espresso. Hope this helps someone else.

    https://gist.github.com/digitalbuddha/d886eae1578bca78b9bf

    Edit:

    There is a much easier way to accomplish this task. Add the following rule to your tests

    public class AsyncTaskSchedulerRule implements TestRule {
        final Scheduler asyncTaskScheduler = Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
    
        @Override
        public Statement apply(Statement base, Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    RxJavaHooks.setOnIOScheduler(scheduler -> asyncTaskScheduler);
                    RxJavaHooks.setOnComputationScheduler(scheduler -> asyncTaskScheduler);
                    RxJavaHooks.setOnNewThreadScheduler(scheduler -> asyncTaskScheduler);
                    try {
                        base.evaluate();
                    } finally {
                        RxJavaHooks.reset();
                    }
                }
            };
        }
    }
    

提交回复
热议问题