【问题标题】:Android Test if toast is shown [duplicate]Android测试是否显示吐司[重复]
【发布时间】:2018-09-13 22:08:46
【问题描述】:

我正在编写一个测试来检查是否正在显示自定义Toast。 Toast 未在 Activity 实例中构建和显示。我创建了一个可以访问Context 的类。

public class ToastHandler{
    private Context context;
    public ToastHander(Context context){
       this.context = context;
    }

   public createToast(DataStructure data){
      // Create and show Custom Toast using data and context
   }
}

我应该如何进行测试?我正在使用浓缩咖啡。

【问题讨论】:

标签: java android unit-testing android-espresso android-toast


【解决方案1】:

怎么样

onView(withText(R.string.toast_text)).inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));

?

如果有这样的需要,你可以像这样创建匹配器

http://baroqueworksdev.blogspot.de/2015/03/how-to-check-toast-window-on-android.html

【讨论】:

  • 如何获得mActivityRule。我不需要启动我想测试仅显示的 Toast 的 Activity。
  • Espresso 用于视图 (UI) 测试,如果您尝试在没有视图的情况下测试 toast,您可能不适合使用 espresso,好吗? AFAIK,您至少需要应用程序上下文来显示 toast,我错了吗?:)
  • 我的意思是你要测试系统行为,而不是你的应用程序,就是这样 - 你的代码只能由 jUnit 测试
  • 我正在使用 espresso,所以我可以访问应用程序的上下文。我无法仅使用 jUnit 进行测试。
  • 我不知道你真的要测试什么?如果调用的该方法显示吐司?是背景/服务案例吗?
猜你喜欢
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2013-10-14
  • 2016-06-16
相关资源
最近更新 更多