Hes*_*sam 2 android android-intent android-espresso
我想测试我从 Activity A 发送到 Activity B 的 Intent。有一些示例,android-testing和espresso.intent.Intents。
不幸的是,我无法让它投入工作。我想在我的第一个活动中测试以下方法。
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);
}
Run Code Online (Sandbox Code Playgroud)
所以,根据我的参考资料,这是我的测试代码:
@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")));
}
}
Run Code Online (Sandbox Code Playgroud)
我在日志中得到了什么:
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}]])
Run Code Online (Sandbox Code Playgroud)
哦,伙计,两天后我终于找到了解决方案。我使用hasComponent而不是toPackage我的测试通过了。我不确定我的结论是否正确,但似乎为了检查我们的应用程序的活动(组件),我们应该使用hasComponent方法。
所以我的改变是:
intended(allOf(
hasExtra(PoiActivity.EXTRA_SEARCH_TYPE, PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION),
hasComponent("com.XXX.passenger.poi.PoiActivity")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3057 次 |
| 最近记录: |