首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使断言等待IdlingResource断言

如何使断言等待IdlingResource断言
EN

Stack Overflow用户
提问于 2015-10-06 21:21:08
回答 3查看 1.7K关注 0票数 3

我想使用空闲资源,因为我在我的应用程序中使用RxJava和EventBus,有时我的测试失败(我认为这是因为同步)。

依赖关系:

代码语言:javascript
复制
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.retrofit:retrofit-mock:1.9.0'
androidTestCompile 'com.squareup.assertj:assertj-android:1.1.0'
androidTestCompile 'com.squareup.spoon:spoon-client:1.2.0'

考试失败:

代码语言:javascript
复制
@RunWith(AndroidJUnit4.class)
@SmallTest
public class PushNotificationTests {

    @Test
    public void testSomething(){

        // do things...
        MyIdlingResource eventBusIdlingResource = new MyIdlingResource();
        registerIdlingResources(eventBusIdlingResource);

        Order.Status status = Order.getCurrentObservable().toBlocking().first().getStatus();
        assertThat(status).isEqualTo(Order.Status.PROGRESS);   // Fail,

        unregisterIdlingResources(eventBusIdlingResource)
    }

}

试着但不起作用:

代码语言:javascript
复制
@RunWith(AndroidJUnit4.class)
@SmallTest
public class PushNotificationTests {

    @Test
    public void testSomething(){

        // do things...

        MyIdlingResource eventBusIdlingResource = new MyIdlingResource();
        registerIdlingResources(eventBusIdlingResource);

        // Wait manually?
        int sleeps = 0;
        while(!eventBusIdlingResource.isIdleNow() || sleeps < 10){
            sleep(100);
            sleeps++;
        }

        Order.Status status = Order.getCurrentObservable().toBlocking().first().getStatus();
        assertThat(status).isEqualTo(Order.Status.PROGRESS);   // Fail,

        unregisterIdlingResources(eventBusIdlingResource)
    }

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-07 17:40:25

最后将睡眠方法封装在类中。

代码语言:javascript
复制
import android.support.test.espresso.IdlingResource;

/**
 * Have functions to sleep the processor because assertions are not linked to
 * {@link IdlingResource} to do assertions, so should be used before asserts if there's an
 * idle process.
 */
public class IdlingResourceSleeper {

    private static final int SLEEPS_LIMIT = 50;
    private static final int SLEEPS_TIME = 10;

    /**
     * Used to sleep {@link IdlingResourceSleeper#SLEEPS_LIMIT} times and
     * {@link IdlingResourceSleeper#SLEEPS_TIME} ms until idlingResource.isIdleNow() is false.
     *
     * @param idlingResource
     */
    public static void sleep(IdlingResource idlingResource) {
        int sleeps = 0;
        while (!idlingResource.isIdleNow() || sleeps < SLEEPS_LIMIT) {
            android.os.SystemClock.sleep(SLEEPS_TIME);
            sleeps++;
        }

    }
}

资料来源:- 要旨 - 帖子

票数 1
EN

Stack Overflow用户

发布于 2017-02-09 17:30:25

在检查UI状态之前,我有相同的要求强制Espresso处于空闲状态。对我起作用的是添加一个始终真实的视图断言,因为Espresso在执行它之前等待空闲状态。

代码语言:javascript
复制
onView(isRoot()).check(matches(anything()));
票数 4
EN

Stack Overflow用户

发布于 2018-08-27 17:52:47

我知道这是个老生常谈的问题,但我在搜索时偶然发现了这个问题,并认为其他人也会这样做。这个行为现在内置到Espresso.onIdle()中。

void onIdle () 循环主线程,直到应用程序空闲。 只为不与任何UI元素交互但需要Espresso的主线程同步的测试调用此方法!此方法主要用于构建在Espresso之上的测试实用程序和框架。 对于UI测试,使用onView(Matcher)或onData(Matcher)。这些Apis已经使用了Espresso的内部同步机制,并且不需要调用onIdle()。

https://developer.android.com/reference/android/support/test/espresso/Espresso#onidle

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32979964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档