2016-05-03 11 views
4

Ich wollte testen, ob ein Dialog Fragment angezeigt wird oder nicht, mit Roboelectric.DialogFragment mit Roboelectric

public class SomeDialogActivity extends FragmentActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     DialogFragment someDialogFragment = new SomeDialogFragment(); 
     someDialogFragment.show(getSupportFragmentManager(), "some_dialog"); 
    } 
} 

Nun wollte ich, wenn dieser Dialog testen, ist so etwas wie dies gezeigt:

@Test 
public void dialogFragmentIsShownToTheUser() { 
     DialogFragment dialog = new SomeDialogFragment(); 
     DialogFragment someDialogFragment = new SomeDialogFragment(); 
     startFragment(someDialogFragment); 

     SomeDialogActivity activity = Robolectric.setupActivity(SomeDialogActivity.class); 

     Dialog dialog = ShadowDialog.getLatestDialog(); 
     assertNotNull(dialog); 
     assertEquals(.... , ....) 
} 

Antwort

3

behandelt es als normale fragment Situation. So überprüfen, ob Fragment wird zum nächsten Code haben müssen gezeigt:

@Test 
public void dialogFragmentIsShownToTheUser() { 
    SomeDialogActivity activity = Robolectric.setupActivity(SomeDialogActivity.class); 

    DialogFragment dialogFragment = (DialogFragment) activity.getSupporFragmetManager() 
             .findFragmentByTag("some_dialog"); 
    assertNotNull(dialogFragment); 
} 
+0

Das Problem ist, sobald die dialogfragment initialisiert wird, es kommt nie zurück in die assertNotNull überprüfen. es ist stecken geblieben und ich bekomme diesen Fehler. android.content.res.Resources $ NotFoundException: Kann Ressource ID # 0x7f030023 \t '... android.content.res.Resources.loadXmlResourceParser (Resources.java) \t bei android.content.res finden. Resources.getLayout (Resources.java:852) \t bei android.view.LayoutInflater.inflate (LayoutInflater.java:394) \t bei android.view.LayoutInflater.inflate (LayoutInflater.java:352) \t bei com.xxx .yyy.zzz.fragments.MacDialogFragment.onCreateDialog (MacDialogFragment.java:36) ..... ' –

Verwandte Themen