如何使断言等待IdlingResource断言 [英] how to make assert to wait for IdlingResource to assert

查看:66
本文介绍了如何使断言等待IdlingResource断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use idling resources because I´m using RxJava and EventBus in my app and sometimes my tests fail (I´m thinking that is because that synchronisation).

依赖项:

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'

测试失败:

@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)
    }

}

尝试但不起作用:

@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)
    }

}

推荐答案

最后将sleep方法封装在一个类中.

End up encapsulating the sleep method inside a class.

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++;
        }

    }
}

来源: -要点 -帖子

这篇关于如何使断言等待IdlingResource断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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