How can I test fragments with Robolectric?

后端 未结 7 1140
萌比男神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 10:53

    Old android fragments are already deprecated, seems like support fragments soon will be deprecated too. To test androidx fragments you can use fragment scenarios with robolectric https://developer.android.com/training/basics/fragments/testing

    testImplementation 'androidx.fragment:fragment-testing:1.2.2'
    
    val scenario = launchFragmentInContainer()
    scenario.onFragment { fragment ->
        assertNotNull(fragment.view.synteticInflatedView)
    }
    

提交回复
热议问题