2

我经常用来Toast.makeText().show()向我的 Android 应用程序的用户显示消息。这些可以是下一步要做什么的说明或错误消息。作为我的 JUnit 测试的一部分,我想包含这些消息按预期出现的断言。我该怎么做呢?

4

2 回答 2

2

我通过使用Robotium框架来做到这一点,并在显示吐司后立即调用它:

     TextView toastTextView = solo.getText(0);
     if(toastTextView != null){
         String toastText = toastTextView.getText().toString();
         assertEquals(toastText, "Your expected text here");
     }
     //wait for Toast to close
     solo.waitForDialogToClose(5000);
于 2013-05-19T13:12:10.177 回答
1

自从最初提出这个问题以来,我发现以下解决方案对我很有效:

public static void waitForToast(Solo solo, String message) {
    Assert.assertTrue(solo.waitForDialogToOpen(TIME_OUT));
    Assert.assertTrue(solo.searchText(message));
}
于 2014-10-29T21:17:37.360 回答