这个问题是关于让Android测试用例独立于运行日期,即使被测试的内容取决于电话/模拟器的系统日期。
我想用Espresso在我的Android应用程序中测试一个视图,这取决于当前的日期。它基本上是一个日历:我有一个ListView,每天有一行,我可以点击一天获得一天的详细视图:
<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
<?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“。当然,当加载月份视图时,将显示当前月份。这将使测试取决于当前日期。
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将测试的日期定在某一天?
发布于 2016-05-24 09:37:18
我有同样的需要使用一个固定的日期时间。我的解决方案是创建一个包装类来访问当前日期/时间。这个类被设计为单例,在我的测试中,我用一个新的实例替换了静态的单例实例。这个新的配置为总是报告相同的“当前”日期/时间,并可以更改为新的日期/时间。
https://stackoverflow.com/questions/37165590
复制相似问题