アンしかし
@Test
public void typeNumber_ValidInput_InitiatesCall() {
// Types a phone number into the dialer edit text field and presses the call button.
onView(withId(R.id.edit_text_caller_number))
.perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard());
onView(withId(R.id.button_call_number)).perform(click());
// Verify that an intent to the dialer was sent with the correct action, phone
// number and package. Think of Intents intended API as the equivalent to Mockito's verify.
intended(allOf(
hasAction(Intent.ACTION_CALL),
hasData(INTENT_DATA_PHONE_NUMBER),
toPackage(PACKAGE_ANDROID_DIALER)));
}
、完全に自動化されたテストの一環として、あなたがする必要があります。その答えは次のようになりますその活動の結果が要件を満たしていることを確認するためにテストコードの後に「意図」メソッドを使用することですユーザーの入力を実際にブロックすることなく実行できるように、アクティビティへのレスポンスをスタブしてください。intenを設定する必要があります
@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 ActivityResult(Activity.RESULT_OK, null));
}
次に、あなたがこのようなテストの対応する部分を書くことができます:テストを実行する前に、トンのスタブ
@Test
public void pickContactButton_click_SelectsPhoneNumber() {
// Stub all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity
// is never launched and result is stubbed.
intending(hasComponent(hasShortClassName(".ContactsActivity")))
.respondWith(new ActivityResult(Activity.RESULT_OK,
ContactsActivity.createResultData(VALID_PHONE_NUMBER)));
// Click the pick contact button.
onView(withId(R.id.button_pick_contact)).perform(click());
// Check that the number is displayed in the UI.
onView(withId(R.id.edit_text_caller_number))
.check(matches(withText(VALID_PHONE_NUMBER)));
}
あなたは電話ダイヤラーなどの他のアプリケーション(から実際のユーザの入力を検証する必要がある場合)、それはエスプレッソの範囲外です。私は現在、このような場合に役立つベンダーと仕事をしているので、名前とツールに名前を付けることを躊躇しますが、多くの人が実際のエンドツーエンドの経験をシミュレートするテストを書く必要があります。
マイクエバンスには、テストの意図についての朗報があります。hereと、常にアンドロイドのドキュメントhereがあります。
あなたの答えは本当にありがたいですが、startActivityを使用してstartActivityForResultを使用せずにダイヤラーアクティビティを開始するという問題を解決する方法を探していました。私はあなたがCamera Intentを嘲笑して言ったのと同じような戦略を使っていた – cammando