How can I test fragments with Robolectric?

后端 未结 7 1144
萌比男神i
萌比男神i 2020-12-02 10:16

I know there is a Robolectric.shadowOf(Fragment) method and a ShadowFragment class, thought they aren\'t listed on the docs, but I can\'t make it w

7条回答
  •  感情败类
    2020-12-02 11:03

    I just wanted to add that in Robolectric 2.0 even after doing:

    activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get();
    fragment.show(activity.getSupportFragmentManager(), null);
    fragment.getDialog();  //This stills returns null
    

    It still returned null for me. what I did was to add activity.getSupportFragmentManager().executePendingTransaction(); and it worked.

    It seems robolectric doesn't run this for some reason. it seems that maybe the Looper is paused or something. any way this worked for me and it looks like this:

    activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get();
    fragment.show(activity.getSupportFragmentManager(), null);
    activity.getSupportFragmentManager().executePendingTransactions();
    fragment.getDialog();
    

提交回复
热议问题