使用改造网络请求时如何使用IdlingResource测试Android UI [英] How to test Android UI using IdlingResource when using Retrofit network requests

查看:87
本文介绍了使用改造网络请求时如何使用IdlingResource测试Android UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写集成测试,这些集成测试将在UI中执行使用 Retrofit 进行网络调用的操作.

I am writing integration tests that perform actions in the UI which start network calls using Retrofit.

我知道我需要实现一个

I know I need to implement a CountingIdlingResource, but I want to do it the correct way (and not reinvent the wheel if it has already been done).

是否有人在其应用程序的 Espresso 测试套件中实现了IdlingResource,以等待网络请求执行?

Has anyone implemented an IdlingResource in their app's Espresso test suite to wait while network requests execute?

更多信息此处.

推荐答案

最简单的解决方案:基本上是将Retrofit的线程池执行器换成AsyncTask之一(这是非常有帮助的Nick推荐的关联了Google网上论坛).我这样做是这样的:

The most straightforward solution for this: is to basically swap out Retrofit's Thread-pool executor with an AsyncTask one (as recommended by the very helpful Nick from that linked Google group discussion). I do this like so:

new RestAdapter.Builder()
               .setEndpoint(LOCLSET_SERVER_URL)
               .setExecutors(AsyncTask.THREAD_POOL_EXECUTOR,
                             new MainThreadExecutor())
               .build();

我不确定这是否是最合适的解决方案,但这是我可以使用的最快,最理智的解决方案.请注意,这仅适用于ICS +.

I'm not sure if this is the most appropriate solution, but it's the quickest most sane one that I could get working. Bare in mind the caveat, that this works only for ICS+.

这篇关于使用改造网络请求时如何使用IdlingResource测试Android UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆