2017-07-18 168 views
1

我正在尝试在Settings中为PreferenceFragments片段编写测试。使用Espresso测试Android PreferenceFragment

不过,我已经收到此错误: android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView

用于测试的代码如下:

@RunWith(AndroidJUnit4.class) 
@SmallTest 
public class SettingsFragmentTest { 

    @Rule 
    public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
      SettingsActivity.class); 

    @Test 
    public void preferredLocationShouldBeVisibleOnDisplay(){ 
     mActivityRule.getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       SettingsFragment settingsFragment = startSettingsFragment(); 
      } 
     }); 

     // This check passes correctly 
     onView(withId(R.id.weather_settings_fragment)) 
       .check(matches(isCompletelyDisplayed())); 

     // This check gives me the NoMatchingViewException 
     onData(allOf(is(instanceOf(Preference.class)), 
       withKey("location"))) 
       .check(matches(isCompletelyDisplayed())); 
    } 

    private SettingsFragment startSettingsFragment(){ 
     SettingsActivity activity = mActivityRule.getActivity(); 
     FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction(); 
     SettingsFragment settingsFragment = new SettingsFragment(); 

     transaction.replace(R.id.weather_settings_fragment, settingsFragment, "settingsFragment"); 
     transaction.commit(); 

     return settingsFragment; 
    } 
} 

的settings_activity布局如下所示:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:name="com.example.android.sunshine.SettingsFragment" 
      android:id="@+id/weather_settings_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

首选项屏幕布局如下:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

    <EditTextPreference 
     android:defaultValue="@string/pref_location_default" 
     android:inputType="text" 
     android:key="@string/pref_location_key" 
     android:singleLine="true" 
     android:title="@string/pref_location_label" /> 

    <ListPreference 
     android:defaultValue="@string/pref_units_metric" 
     android:entries="@array/pref_units_options" 
     android:entryValues="@array/pref_units_values" 
     android:key="@string/pref_units_key" 
    android:title="@string/pref_units_label" /> 

<CheckBoxPreference 
    android:defaultValue="@bool/show_notifications_by_default" 
    android:key="@string/pref_enable_notifications_key" 
    android:summaryOff="@string/pref_enable_notifications_false" 
    android:summaryOn="@string/pref_enable_notifications_true" 
    android:title="@string/pref_enable_notifications_label" /> 

</PreferenceScreen> 

我一直没有找到任何有关如何测试PreferenceFragments的示例或信息。大部分与测试活动有关的信息。

回答

0

我通常通过Espresso的活动测试碎片。只需测试你的Fragment所暴露的UI,就好像它是在ActivityTestRule开始的Activity中一样。如果在初始启动活动时Fragment不存在,请按照用户启动Fragment事务的相同方式在测试中导航。如果你发现自己需要测试业务逻辑,需要你在某种测试工具中站立碎片,那通常是一个很好的指标,它应该被拉到一个单独的类中,可以(理想情况下)单元测试,没有任何Android依赖。

相关问题