问题描述
我想测试我是从活动A发送到活动B.的意图
有样本的是,<一个href=\"https://github.com/googlesamples/android-testing/blob/master/ui/es$p$psso/IntentsBasicSample/app/src/androidTest/java/com/example/android/testing/es$p$psso/BasicSample/DialerActivityTest.java\"相对=nofollow> Android的测试和的。
I want to test the Intent I'm sending from Activity A to Activity B.There are samples for that, android-testing and espresso.intent.Intents.
不幸的是,我无法得到它变成工作:(
我想测试下我的第一个活动的方法。
Unfortunately, I couldn't get it into work :(I want to test following method in my first Activity.
private void searchForDropOff()
{
this.startActivityForResult(PoiActivity.newIntent(this, PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION,
this.mBooking.getPickUp() != null ? this.getPickUp().getSafeLatLng() : this.mReferenceLatLng),
PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION);
}
所以,根据我引用这是我的测试code:
So, according to my references this is my test code:
@RunWith(AndroidJUnit4.class)
public class FirstActivityTest
{
@Rule
public final IntentsTestRule<FirstActivityTest> mRule = new IntentsTestRule<>(FirstActivityTest.class);
@Before
public void stubAllExternalIntents()
{
// By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
// every test run. In this case all external Intents will be blocked.
intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
}
@Test
public void click_drop_off_box()
{
// Click drop-off box, POI activity displays
onView(withId(R.id.booking_drop_off_layout)).perform(click());
// Verify that an intent to the dialer was sent with the package.
// Think of Intents intended API as the equivalent to Mockito's verify.
intended(allOf(
hasExtra(PoiActivity.EXTRA_SEARCH_TYPE, PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION),
toPackage("com.XXX.passenger.poi.PoiActivity")));
}
}
什么我得到的日志:
What I'm getting in Log:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: (has extras: has bundle with: key: is "addressType" value: is <2> and resolvesTo: com.xxx.passenger.poi.PoiActivity)
Matched intents:[]
Recorded intents:
-Intent { cmp=com.xxx.passenger/com.xxx.passenger.poi.PoiActivity (has extras) } handling packages:[[com.xxx.passenger]], extras:[Bundle[{referencePoint=lat/lng: (1.3650683,103.8313499), addressType=2}]])
我糊涂了很多:(任何想法是AP preciated,谢谢。
I'm confused a lot :( any idea would be appreciated. Thanks.
推荐答案
哦,最后两天后,我找到了解决办法。
我使用 hasComponent
而不是 toPackage
和我的测试通过。
我不知道我的结论是正确的,但似乎是检查我们的应用程序的活动(组件)我们应该使用 hasComponent
方法。
oh man, finally after two days I found the solution.I Used hasComponent
instead of toPackage
and my test passed.I'm not sure my conclusion is right but seems for checking our application's activities (Components) we should use hasComponent
method.
所以我的变化是:
intended(allOf(
hasExtra(PoiActivity.EXTRA_SEARCH_TYPE, PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION),
hasComponent("com.XXX.passenger.poi.PoiActivity")));
这篇关于如何基于Android中居preSSO测试意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!