Ho un frammento Android che voglio testare. Ho creato un'attività di test a cui aggiungo questo frammento ed eseguo alcuni test dell'Espresso.Come testare un frammento usando Espresso
Tuttavia, Espresso non trova nessuna vista all'interno del frammento. Scarica la gerarchia della vista ed è tutto vuoto.
Non voglio utilizzare l'attività padre effettiva. Voglio solo testare questo frammento da solo. Qualcuno ha fatto questo? C'è un campione che ha un codice simile?
@RunWith(AndroidJUnit4.class)
class MyFragmentTest {
@Rule
public ActivityTestRule activityRule = new ActivityTestRule<>(
TestActivity.class);
@Test
public void testView() {
MyFragment myFragment = startMyFragment();
myFragment.onEvent(new MyEvent());
// MyFragment has a recyclerview.
//OnEvent is EventBus callback that in this test contains no data.
//I want the fragment to display empty list text and hide the recyclerView
onView(withId(R.id.my_empty_text)).check(matches(isDisplayed()));
onView(withId(R.id.my_recycler)).check(doesNotExist()));
}
private MyFragment startMyFragment() {
FragmentActivity activity = (FragmentActivity) activityRule.getActivity();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
MyFragment myFragment = new MyFragment();
transaction.add(myFragment, "myfrag");
transaction.commit();
return myFragment;
}
}
Hai provato a fare ricerche sul Web per questo? Trovo molti esempi –
È sempre una buona idea mostrarci il tuo codice. – Emzor
Ho provato a cercare in rete. Non sono stato in grado di trovare alcun esempio. @DougStevenson, sarei grato se potessi condividere ciò che hai trovato. – greenrobo