gpt4 book ai didi

android - 如果添加第二个测试,Espresso toast 测试将失败

转载 作者:行者123 更新时间:2023-11-29 02:34:58 31 4
gpt4 key购买 nike

我有一个 Activity 可以根据网络服务调用返回的状态显示不同的消息框。

我正在编写一个测试类,我的第一个测试测试是否在出现网络错误时显示其中一个 toast。下面是测试类:

@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends BaseTest {

@Rule
public final ActivityTestRule<LoginActivity> login = new ActivityTestRule(LoginActivity.class, true);

@Test
public void noNetwork() {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
login.getActivity().onEventMainThread(new LoginResp(CopiaWebServiceClient.ResponseStatus.NETWORK_ERROR));
}
});

onView(withText(R.string.toast_no_network_connection))
.inRoot(withDecorView(not(is(login.getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
}
}

所以 noNetwork测试通话 LoginActivityonEventMainThread(LoginResp loginResp)方法(这需要在 UI 线程上运行,因此我使用 runOnMainSync )和网络错误状态以提示它显示预期的 toast_no_network_connection toast。

此测试有效并运行并成功通过。

这是我的问题:

如果我向测试类添加第二个测试,则第二个测试失败。这是第二个测试,与第一个相同,只是它向 onEventMainThread(LoginResp loginResp) 传递了不同的错误状态。这样就会显示不同的 toast :

    @Test
public void serverErr() {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
login.getActivity().onEventMainThread(new LoginResp(CopiaWebServiceClient.ResponseStatus.HTTP_SERVER_ERROR));
}
});

onView(withText(R.string.toast_operation_failure_app_error))
.inRoot(withDecorView(not(is(login.getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
}

第二个测试失败并输出:android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131689669>[toast_operation_failure_app_error] value: Sorry, failure to complete operation due to application error.

但是,在运行测试的同时观察模拟器,我看到了初始的 toast_no_network_connection toast(第一次测试所期望的),然后是 toast_operation_failure_app_error toast(第二次测试所期望的)。为什么第二次测试失败了?

这与一个接一个运行的测试有关,因为当我注释掉第一个测试时,第二个测试通过。

我的 onEventMainThread(LoginResp loginResp) LoginActivity 中的方法具有以下代码,可根据状态显示适当的 toast:

switch (status) {

case HTTP_UNAUTHORIZED:
DialogCreator.createAlertDialog(this, getString(R.string.dialog_msg_login_fail)).show();

break;

case NETWORK_ERROR:
Toast.makeText(this, getString(R.string.toast_no_network_connection), Toast.LENGTH_SHORT).show();

break;

default:
Toast.makeText(this, getString(R.string.toast_operation_failure_app_error), Toast.LENGTH_SHORT).show();

}

调试测试我看到第一个测试按预期进入了 NETWORK_ERROR 情况,第二个测试进入了 switch 语句的 default部分,也符合预期。

最佳答案

在进一步测试之后,我选择了 Eirik W 在 Espresso checking if toasts are displayed (one on top of another) 的回答中建议的方法。

我稍微更改了我的生产代码,将 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) 注释的 Toast 成员添加到 LoginActivity 以启用 LoginActivityTest 中的 @After 注释方法取消每次测试后显示的任何 toast。

关于android - 如果添加第二个测试,Espresso toast 测试将失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596959/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com