首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定Android-Espresso测试用例的日期

如何确定Android-Espresso测试用例的日期
EN

Stack Overflow用户
提问于 2016-05-11 14:24:42
回答 1查看 1.7K关注 0票数 2

这个问题是关于让Android测试用例独立于运行日期,即使被测试的内容取决于电话/模拟器的系统日期。

我想用Espresso在我的Android应用程序中测试一个视图,这取决于当前的日期。它基本上是一个日历:我有一个ListView,每天有一行,我可以点击一天获得一天的详细视图:

代码语言:javascript
复制
    <ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:headerDividersEnabled="false"/>

但我的问题的有趣之处是ActionBar

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/cp_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:showIn="@layout/activity_month_time_record">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>

</android.support.v7.widget.Toolbar>

您所看到的TextView将显示选定日期的日期,例如"Wed,4. May '16“。当然,当加载月份视图时,将显示当前月份。这将使测试取决于当前日期。

代码语言:javascript
复制
onData(anything()).inAdapterView(withId(android.R.id.list)).atPosition(5)
            .perform(click());

如果我在2016年5月运行这个测试,那么ActionBar将显示"Wed,4.5.16“,而如果我在2016年6月运行这个测试,则将显示"Sat,4.Jun '16”。

那么,我如何告诉Espresso将测试的日期定在某一天?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-24 09:37:18

我有同样的需要使用一个固定的日期时间。我的解决方案是创建一个包装类来访问当前日期/时间。这个类被设计为单例,在我的测试中,我用一个新的实例替换了静态的单例实例。这个新的配置为总是报告相同的“当前”日期/时间,并可以更改为新的日期/时间。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37165590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档