Android的单元测试:测试活动前结算preFS [英] android unit test: clearing prefs before testing activity
问题描述
我要确保preference是在我的单元测试开始了我的活动前清零。
I want to ensure that a preference is cleared before my Activity
is started in my unit test.
问题是,清除preferences,你需要调用 getActivity()
。在此之后,活动
启动时,它读取preferences。
The problem is that to clear the preferences, you need to call getActivity()
. After that, the Activity
is started, which reads the preferences.
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(mActivity);
prefs.edit().clear().commit();
}
在 getActivity()
被调用时,活动
创建,它读取$ P $的价值PF,先下一行清除preF。
When getActivity()
is called, the Activity
is created, which reads the value of the pref, before the next lines clear the pref.
有没有办法获得上下文
对象,而无需启动活动
Is there a way obtaining a Context
object without starting the Activity
?
我是新来的Android单元测试,所以也许我失去了一些基本的东西。
I'm new to Android unit tests, so maybe I'm missing something basic.
感谢。
推荐答案
嗯,找到了答案在这里,
<一href=\"http://stackoverflow.com/questions/5544205/accessing-application-context-from-testsuite-in-setup-before-calling-getactivi\">Accessing从TestSuite的在安装应用程序上下文()调用getActivity()之前
eh, found the answer here, Accessing application context from TestSuite in Setup() before calling getActivity()
调用
this.getInstrumentation().getTargetContext()
这篇关于Android的单元测试:测试活动前结算preFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!