小编Big*_*opa的帖子

使用活动场景规则测试 Toast

我目前是 Android 编程新手,我正在尝试使用 espresso 测试部分 UI。目前,我在测试 toast 消息时遇到问题。

Toast toast = Toast.makeText(context, R.string.some_message, duration);
toast.show();
Run Code Online (Sandbox Code Playgroud)

然后在我的测试文件中,我定义了活动场景规则。

public ActivityScenarioRule<CreateActivity> activityScenarioRule =
            new ActivityScenarioRule<>(SomeActivity.class);
Run Code Online (Sandbox Code Playgroud)

最后,我测试这部分得到的最佳解决方案如下所示。

ActivityScenario<SomeActivity> scenario = activityScenarioRule.getScenario();
scenario.onActivity(activity ->
                Espresso.onView(ViewMatchers.withText(R.string.error_message)).inRoot(RootMatchers.withDecorView
                        (not(is(activity.getWindow().getDecorView())))).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
);
Run Code Online (Sandbox Code Playgroud)

当我运行测试时它永远不会停止。任何帮助都会有用的。:)

testing android unit-testing toast android-espresso

5
推荐指数
0
解决办法
569
查看次数

标签 统计

android ×1

android-espresso ×1

testing ×1

toast ×1

unit-testing ×1