如何在Espresso中没有实际启动活动的情况下检查发送的预期意图? [英] How to check expected intent sent without actually launching activity in Espresso?

查看:100
本文介绍了如何在Espresso中没有实际启动活动的情况下检查发送的预期意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UI测试,该测试单击一个按钮,然后在其onClickListener中启动一个新的Activity.该测试检查是否发送了预期的意图.

I have a UI test which clicks a button, and then launch a new Activity in its onClickListener. The test checks whether expected intent is sent or not.

我的问题是,我想测试是否在没有实际启动活动的情况下发送了预期意图.因为我发现新活动会初始化其状态,并且使后续测试变得不稳定.

My problem is, I want to test whether expected intent is sent without actually launching the activity. Because I found that new activity initializes its state, and it makes subsequent tests flaky.

我知道有两个 Espresso意向 api,它们是intendedintending,但是都不能满足我的需求. intended api实际上会启动目标活动,而intending api不会启动该活动,但是会调用onActivityResult回调,我也不想要.因为恐怕onActivityResult中的代码可能会导致另一种脆弱.同样,intending不会断言是否发送了匹配意图,它只是在找到匹配意图时才调用onActivityResult回调,这意味着我必须检查是否调用了onActivityResult

I know there are two Espresso Intents api, which are intended and intending, but both fail to meet my needs. intended api actually launches the target activity, and intending api doesn't launch the activity, but it calls onActivityResult callback which I don't want either. Because I'm afraid that code inside onActivityResult may cause another flakiness. Also intending doesn't assert whether matching intent is sent, it just calls onActivityResult callback when matching intent is found, which means I have to check whether onActivityResult is called or not!

有什么干净的方法可以实现我想要的吗?

Is there any clean way to achieve what I want?

推荐答案

Espresso的Intents类是一种简洁易用的api,但是当它不能满足您的需要时,还有另一种选择.如果使用AndroidJUnit4测试运行程序,则可以使用InstrumentationRegistry.getInstrumentation()获取Instrumentaion实例,然后可以添加Instrumentation.ActivityMonitor实例.

Espresso's Intents class is a concise and handy api, but when it doesn't meet your needs, there is an alternative. If you use AndroidJUnit4 test runner, you can get Instrumentaion instance using InstrumentationRegistry.getInstrumentation(), and then you can add Instrumentation.ActivityMonitor instance.

Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("YOUR_ACTIVITY", null, true);
InstrumentationRegistry.getInstrumentation().addMonitor(am);
onView(withId(R.id.view_id_to_perform_clicking)).check(matches(isDisplayed())).perform(click());
assertTrue(InstrumentationRegistry.getInstrumentation().checkMonitorHit(am, 1));

ActivityMonitor构造函数的第三个参数告诉我们要阻止活动启动.注意,这种方法有其局限性.与Espresso Intents的丰富匹配器支持,您不能为ActivityMonitor设置多个条件.

The third parameter of ActivityMonitor constructor tells we want to block activity launching. Note that this approach has its limitation. In contrast to Espresso Intents' rich Matcher support, You can not set multiple condition for ActivityMonitor.

您可以在 ApiDemos 中找到几个示例.在ContactsSelectInstrumentation类中.

You can find several samples in ApiDemos, especially in ContactsSelectInstrumentation class.

这篇关于如何在Espresso中没有实际启动活动的情况下检查发送的预期意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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