2016-02-04 42 views
1

我想測試我從活動A發送到活動B的意圖。 有這方面的示例,android-testingespresso.intent.Intents。 。如何在Android中測試基於Espresso的Intent?

不幸的是,我不能讓它進入工作:( 我想測試下我的第一個活動的方法

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

所以,根據我引用這是我的測試代碼:

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

什麼我得到的日誌:

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

我很困惑很多:(任何想法將不勝感激。謝謝。

回答

4

噢男人,終於兩天後我找到了解決辦法。 我用hasComponent而不是toPackage,我的測試通過。 我不確定我的結論是否正確,但似乎對於檢查我們的應用程序的活動(組件),我們應該使用hasComponent方法。

所以我的變化是:

intended(allOf(
       hasExtra(PoiActivity.EXTRA_SEARCH_TYPE, PlacesAPIRequest.PARAM_SEARCH_TYPE_DESTINATION), 
       hasComponent("com.XXX.passenger.poi.PoiActivity"))); 
+1

是,'package'(又名'applicationId')是的'components'(在清單中聲明,即東西)的分組。 – TWiStErRob

相關問題