Robolectric 启动一个具有观察者的片段

问题描述

如何使用 Robolectric 在测试范围内使用 LiveData 观察者启动片段

片段

class MyFragment(private val viewmodel: Myviewmodel) : Fragment() {

    ...

    fun myObserver {
        ... 
        // If I remove this observer the test will pass. 
        viewmodel.MyLiveData.observe(viewLifecycleOwner,Observer{
            ...
        }
    }
}

我的测试使用 RobolectricTestRunner,因此我可以在测试范围内启动片段。

@RunWith(robolectricTestRunner::class) 
class MyFragmentTest {

    // Executes tasks in the Architecture Components in the same thread
    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    @Test
    fun testOne() {
        val viewmodel: Myviewmodel = mock(Myviewmodel::class.java)
        val scenario = launchFragmentInContainer(
            factory = MainFragmentFactory(viewmodel),fragmentArgs = null
            themeResId = R.style.Theme_MyTheme
        )
        // Tried implementing shadowOf as the error suggests. 

    } 
}

我在尝试运行测试时收到以下错误。我尝试在实例化 FragmentScenario 之前和之后将 Main looper 设置为 idle。

java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.

我已经尝试了以下

  • 为 Main Looper 实现一个影子类。使用 Looper 模式注释类。
@RunWith(RobolectricTestRunner::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyFragmentTest {
    scenario.movetoState(Lifecycle.State.CREATED)
    scenario.movetoState(Lifecycle.State.RESUMED)

我的测试依赖项。

    // Test
    testImplementation 'androidx.arch.core:core-testing:2.1.0'
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3'
    testImplementation "androidx.test.ext:junit-ktx:1.1.2"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3"
    testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    testImplementation "org.robolectric:robolectric:4.5.1"
    testImplementation "org.mockito:mockito-android:2.28.2"

    // Testing Fragments
    debugImplementation "androidx.fragment:fragment-testing:1.3.2"

我用来寻找解决方案的链接' Testing LiveData Transformations? https://jeroenmols.com/blog/2019/01/17/livedatajunit5/

解决方法

我查看了您在 github here 上的存储库。这是我找到的。

问题 1

您的第一个问题是模拟 ViewModel。因此,当您为 onResume 模拟 Fragment 时,它会调用:

fun liveDataObserver() {
    viewModel.scoreLiveData.observe(viewLifecycleOwner,{
        // 
    } )
}

由于 viewModel 是模拟的,因此 scoreLiveDatanull 并且您获得了 NPE。

为了解决这个问题,您还模拟了 scoreLiveData 方法,以便它返回一些可接受的结果:

...
    val liveData = MutableLiveData<Int>().apply { value = 3 }
    val viewModel = mock(MyViewModel::class.java)
    doReturn(liveData).`when`(viewModel).scoreLiveData
...

这将完全修复您的 testOne,但还不能修复 testTwo

问题 2

它仅与您的 testTwo 方法有关。问题是您在 liveDataObserver() 块中调用了 also,并且在 Fragment's 中设置 viewLifecycleOwner 之前调用了它:>

onCreateView

我不确定您要在这里测试什么,但是如果您想验证是否可以在创建 ... scenario = launchFragmentInContainer { MyFragment(viewModel).also { it.liveDataObserver() } } ... Fragment's 后开始观察,您可以执行以下操作:

View

完整代码

@RunWith(RobolectricTestRunner::class) 类 MyFragmentTest { 私有 lateinit var 场景:FragmentScenario

    ...
    // make sure your Fragment is started
    scenario = launchFragmentInContainer (
        factory = MainFragmentFactory(viewModel),initialState = Lifecycle.State.STARTED
    )
    // call liveDataObserver on it
    scenario.withFragment {
        this.liveDataObserver()
    }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...