2017-04-07 10 views
0

エスプレッソを使用して次のコードのテストケースを書く方法を教えてください。私はアイコンをクリックすると実行される次のコードを持っています。 私はtoPackage(....そしておそらくそれはstartActivityForResultを経由して起動されますが、この場合の処理​​方法ならば打ち上げ意図の結果をあざける(私が意図した使用目的の立ち上げを確認することができることを知っている。エスプレッソを使用したテスト

try { 
intent = new Intent(Intent.ACTION_DIAL); 
intent.setData(Uri.parse("tel:" +"xxxxxx"); // 12 digit mobile no 
    if (intent.resolveActivity(context.getPackageManager()) != null) { 
         startActivity(intent) 
     } 
    } 
catch (Exception e) { 
    Toast.makeText(getActivity(), "No phone number available", Toast.LENGTH_SHORT).show(); 
       } 

答えて

2

アンしかし

@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があります。

+0

あなたの答えは本当にありがたいですが、startActivityを使用してstartActivityForResultを使用せずにダイヤラーアクティビティを開始するという問題を解決する方法を探していました。私はあなたがCamera Intentを嘲笑して言ったのと同じような戦略を使っていた – cammando

関連する問題