2016-08-18 9 views
1

私はListFragmentでアクティビティを持っています。 シナリオを実装する必要があります(エスプレッソの助けを借りて):データアイテムがEspressoのListFragmentにないことをアサートする方法は?

  1. 新しいアイテムをListFragmentに追加してください。
  2. 表示されていることを確認してください。
  3. このアイテムを削除してください。
  4. 表示されていないことを確認してください。

私は4番目の問題点があります。 私は記事[https://google.github.io/android-testing-support-library/docs/espresso/advanced/#asserting-that-a-view-is-not-present][1]を読んだ。

private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) { 
     return new TypeSafeMatcher<View>() { 
    @Override 
    public void describeTo(Description description) { 
     description.appendText("with class name: "); 
     dataMatcher.describeTo(description); 
    } 

    @Override 
    public boolean matchesSafely(View view) { 
     if (!(view instanceof AdapterView)) { 
     return false; 
     } 
     @SuppressWarnings("rawtypes") 
     Adapter adapter = ((AdapterView) view).getAdapter(); 
     for (int i = 0; i < adapter.getCount(); i++) { 
     if (dataMatcher.matches(adapter.getItem(i))) { 
      return true; 
     } 
     } 
     return false; 
    } 
    }; 
} 

そして呼び出します:

onView(withId(R.id.list)) 
     .check(matches(not(withAdaptedData(withItemContent("item: 168"))))); 

問題は、私の場合、私はAdapterViewを持っていないということです

著者はマッチャーを実装するためにrecomends。 ListFragmentのidをwithId()関数で渡します。したがって、マッチャーは動作しません。 問題は、withAdaptedDataのコードをListFragmentで動作するように変更する方法です。それとも、私の仕事を解決する別の方法がありますか?

答えて

0

残念ながら、私はこの問題を解決するためにエスプレッソを使用しないよりも優れたものを見つけられませんでした。私は、データがListFragmentにないことを確認するメソッドを実装しています。

private void checkDataNotExistInList(String text) { 
     MyListFragment myListFragment = (MyListFragment)mActivity.getFragmentManager() 
       .findFragmentById(R.id.frgMyList); 
     assertNotNull(myListFragment); 
     ListAdapter listAdapter = (ListAdapter) myListFragment.getListAdapter(); 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      assertThat("Item is in the list", text, is(not(listAdapter.getItem(i).getName())));     
     } 
    } 

しかし、私は良い解決策があると信じています。

関連する問題