首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Expresso测试首选项片段

用Expresso测试首选项片段
EN

Stack Overflow用户
提问于 2016-07-20 10:24:02
回答 1查看 2.4K关注 0票数 2

我在用Expresso测试我的代码时遇到了问题。我写了这段代码:

代码语言:javascript
复制
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}


public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
}

XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preferences_xml">
<PreferenceCategory
  android:title="@string/application_settings"
  android:id="@+id/application_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_processing_private_data_preference"
    android:title="@string/title_processing_private_data_preference"
    android:summary="@string/summary_processing_private_data_preference"
    android:defaultValue="true"
    android:dependency="@string/key_recording_audio_preference"
    android:id="@+id/private_data_check_box_preference"/>
</PreferenceCategory>

 <PreferenceCategory
  android:title="@string/device_settings"
  android:id="@+id/device_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_recording_audio_preference"
    android:title="@string/title_recording_audio_preference"
    android:summary="@string/summary_recording_audio_preference"
    android:defaultValue="true"
    android:id="@+id/recording_audio_check_box_preference"/>

  </PreferenceCategory>
</PreferenceScreen>

我的测试规则:

代码语言:javascript
复制
@Rule
public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
        SettingsActivity.class);

我试着先测试一下复选框:

代码语言:javascript
复制
onData(anything())
            .inAdapterView(allOf(
                    isDescendantOfA(withId(R.id.preferences_xml)),
                    withId(R.id.application_settings_preferences)))
            .atPosition(1)
            .check(matches(isDisplayed()));

测试总是失败与NoMatchingViewException:没有在层次结构中找到匹配的视图:

http://prntscr.com/bv8xlb

我也试着:

代码语言:javascript
复制
String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference);
   onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 

NullPointerException的测试总是失败:

http://prntscr.com/bv8w04

代码语言:javascript
复制
onView(withId(R.id.working_in_background_check_box_preference)).check(matches(isDisplayed()));

NoMatchingViewException也失败了。

会有人用正确的测试用例展示一个例子吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-01 12:18:36

(代表“任择议定书”张贴)。

已解决:

代码语言:javascript
复制
onData(allOf(
            is(instanceOf(Preference.class)),
            withKey(key),
            withSummary(R.string.summary_working_in_background_preference),
            withTitle(R.string.title_working_in_background_preference)))
            .onChildView(withText(title))
            .check(matches(isCompletelyDisplayed()));
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38478692

复制
相关文章

相似问题

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