如果没有按钮,Espresso不会记录任何意图 [英] Espresso does no record any intent if there are no buttons

查看:97
本文介绍了如果没有按钮,Espresso不会记录任何意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个测试以验证使用espresso启动意图的问题,问题在于,意图()没有记录任何意图.

I'm trying to write a test to verify intent launching with espresso, the problem is that intended() does not record any intent.

我有这个测试

  @Test
public void shoulddosomething(){
    startActivity();
    intended(hasComponent(hasClassName(TemplatePictureCaptureActivity.class.getName())));

}

在我的活动中,我有此代码

and in my activity i have this code

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(onRequestLayout());
    Intent intent = new Intent(this, TemplatePictureCaptureActivity.class);
    startActivity(intent);
}

测试结果是这个.

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "cat.helm.recertel.ui.templatepicturecapture.TemplatePictureCaptureActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:[]

我试图在onClickListen内启动意图,但它起作用了,但是如果没有它,我将无法使它起作用.我也尝试过空转资源,但是没有运气.你知道如何做到这一点吗?

I have tried to launch the intent inside onClickListen and it worked, but without it i can't get it to work. I also tried with idling resources with no luck. Do you know how to achieve this?

推荐答案

这可能与Intents组件初始化上的竞争条件有关,而不与startActivity调用与intended的使用之间的竞争条件有关.如果从SUT活动onCreateonResume方法开始活动,则应查看以下测试规则.

This might be related to a race condition on the Intents component initialization and not with a race condition between the startActivity call and the usage of intended. If you start your activity from the SUT activity onCreate or onResume methods you should take a look at the following test rule.

我创建了一个IntentsTestRule来解决此问题. https://gist.github.com/pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b

I've created an IntentsTestRule fixing this issue. https://gist.github.com/pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b

package com.aplazame.utils

import android.app.Activity
import androidx.test.espresso.intent.Intents
import androidx.test.rule.ActivityTestRule

class ExhaustiveIntentsTestRule<T : Activity> : ActivityTestRule<T> {

    private var isInitialized: Boolean = false

    constructor(activityClass: Class<T>) : super(activityClass)

    constructor(activityClass: Class<T>, initialTouchMode: Boolean) : super(activityClass, initialTouchMode)

    constructor(activityClass: Class<T>, initialTouchMode: Boolean, launchActivity: Boolean) : super(
        activityClass,
        initialTouchMode,
        launchActivity
    )

    override fun beforeActivityLaunched() {
        super.beforeActivityLaunched()
        Intents.init()
        isInitialized = true
    }

    override fun afterActivityFinished() {
        super.afterActivityFinished()
        if (isInitialized) {
            // Otherwise will throw a NPE if Intents.init() wasn't called.
            Intents.release()
            isInitialized = false
        }
    }
}

与AndroidX中实现的原始IntentsTestRule的主要区别在于Intents.init()初始化.在开始SUT活动之前,将调用此时间.请记住,此测试规则还将记录用于启动SUT活动的意图.

The main difference with the original IntentsTestRule implemented in AndroidX is the Intents.init() initialization. This time is invoked before starting the SUT activity. Keep in mind that this test rule will also record the intent used to start the SUT activity.

这篇关于如果没有按钮,Espresso不会记录任何意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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