Arv*_*nda 5 android unit-testing robolectric android-testing
是否有任何解决方法可以让我们android.support.v7.app.AlertDialog在Robolectric 中进行测试?
someActivity.findViewById(R.id.alet_btn).performClick();
AlertDialog alert = ShadowAlertDialog.getLatestAlertDialog();
ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(alert);
assertThat(shadowAlertDialog.getTitle()).isEqualTo("Hello");
Run Code Online (Sandbox Code Playgroud)
尝试下面的方法
List shownDialogs = ShadowAlertDialog.getShownDialogs();
if (shownDialogs.get(0) instanceof AlertDialog) {
AlertDialog dialog = (android.support.v7.app.AlertDialog) shownDialogs.get(0);
assertThat(dialog).isShowing();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick();
}
Run Code Online (Sandbox Code Playgroud)
或者
if (ShadowAlertDialog.getLatestDialog() instanceof AlertDialog) {
AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog();
assertThat(dialog).isShowing();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
Run Code Online (Sandbox Code Playgroud)
它允许您单击按钮,但无法验证标题或消息 AFAIK。
| 归档时间: |
|
| 查看次数: |
950 次 |
| 最近记录: |