22

最近作为 Kotlin 协程的一部分引入了该类。StateFlow

我目前正在尝试它并在尝试对我的 ViewModel 进行单元测试时遇到问题。我想要实现的目标:测试我StateFlow在 ViewModel 中以正确的顺序接收所有状态值。

我的代码如下:

视图模型:

class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() {

val userWallet: StateFlow<State<UserWallets>> get() = _userWallets
private val _userWallets: MutableStateFlow<State<UserWallets>> =
    MutableStateFlow(State.Init)

fun getUserWallets() {
    viewModelScope.launch {
        getUserWallets.getUserWallets()
            .onStart { _userWallets.value = State.Loading }
            .collect { _userWallets.value = it }
    }
}

我的测试:

    @Test
fun `observe user wallets ok`() = runBlockingTest {
    Mockito.`when`(api.getAssetWallets()).thenReturn(TestUtils.getAssetsWalletResponseOk())
    Mockito.`when`(api.getFiatWallets()).thenReturn(TestUtils.getFiatWalletResponseOk())

    viewModel.getUserWallets()

    val res = arrayListOf<State<UserWallets>>()
    viewModel.userWallet.toList(res) //doesn't works

    Assertions.assertThat(viewModel.userWallet.value is State.Success).isTrue() //works, last value enmited
}

访问发出的最后一个值有效。但我要测试的是所有发出的值都以正确的顺序发出。使用这段代码:viewModel.userWallet.toList(res) //doesn't works 我收到以下错误:

java.lang.IllegalStateException: This job has not completed yet
    at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1189)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
    at WalletViewModelTest.observe user wallets ok(WalletViewModelTest.kt:52)
....

我想我错过了一些明显的东西。但不确定为什么当我刚刚开始使用 Coroutine 和 Flow 时,当不使用我已经使用的 runBlockingTest 时似乎会发生此错误。

编辑:作为临时解决方案,我将其作为实时数据进行测试:

    @Captor
    lateinit var captor: ArgumentCaptor<State<UserWallets>>

    @Mock
    lateinit var walletsObserver: Observer<State<UserWallets>>

    @Test
    fun `observe user wallets ok`() = runBlockingTest {
        viewModel.userWallet.asLiveData().observeForever(walletsObserver)

        viewModel.getUserWallets()

        captor.run {
            Mockito.verify(walletsObserver, Mockito.times(3)).onChanged(capture())
            Assertions.assertThat(allValues[0] is State.Init).isTrue()
            Assertions.assertThat(allValues[1] is State.Loading).isTrue()
            Assertions.assertThat(allValues[2] is State.Success).isTrue()
        }
    }
4

4 回答 4

12

SharedFlow/StateFlow 是一个热流,如文档中所述,A shared flow is called hot because its active instance exists independently of the presence of collectors.这意味着启动流集合的范围不会自行完成。

要解决此问题,您需要取消调用 collect 的范围,并且由于您的测试范围是测试本身,因此无法取消测试,因此您需要在不同的作业中启动它。

@Test
fun `Testing a integer state flow`() = runBlockingTest{
    val _intSharedFlow = MutableStateFlow(0)
    val intSharedFlow = _intSharedFlow.asStateFlow()
    val testResults = mutableListOf<Int>()

    val job = launch {
        intSharedFlow.toList(testResults)
    }
    _intSharedFlow.value = 5

    assertEquals(2, testResults.size)
    assertEquals(0, testResults.first())
    assertEquals(5, testResults.last())
    job.cancel()
}

您的具体用例:

@Test
fun `observe user wallets ok`() = runBlockingTest {
    whenever(api.getAssetWallets()).thenReturn(TestUtils.getAssetsWalletResponseOk())
    whenever(api.getFiatWallets()).thenReturn(TestUtils.getFiatWalletResponseOk())

    viewModel.getUserWallets()

    val result = arrayListOf<State<UserWallets>>()
    val job = launch {
        viewModel.userWallet.toList(result) //now it should work
    }

    Assertions.assertThat(viewModel.userWallet.value is State.Success).isTrue() //works, last value enmited
    Assertions.assertThat(result.first() is State.Success) //also works
    job.cancel()
}

两个重要的事情:

  1. 始终取消您创建的工作以避免java.lang.IllegalStateException: This job has not completed yet
  2. 由于这是一个 StateFlow,当开始收集(内部toList)时,您会收到最后一个状态。但是,如果您首先开始收集并在调用您的函数之后viewModel.getUserWallets(),那么在result列表中,您将拥有所有状态,以防您也想对其进行测试。
于 2021-01-26T20:59:42.737 回答
3

runBlockingTest只是跳过您的情况下的延迟,但不会用您的测试调度程序覆盖 ViewModel 中使用的调度程序。您需要注入TestCoroutineDispatcher到您的 ViewModel 中,或者由于您使用的是默认情况下viewModelScope.launch {}已经使用的,您需要通过. 您可以创建以下规则并将其添加到您的测试文件中。Dispatchers.MainDispatchers.setMain(testCoroutineDispatcher)

class MainCoroutineRule(
        val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()
    }
} 

在你的测试文件中

@get:Rule
var mainCoroutineRule = MainCoroutineRule()

@Test
fun `observe user wallets ok`() = mainCoroutineRule.testDispatcher.runBlockingTest {
}

顺便说一句,注入调度程序始终是一个好习惯。例如,如果您一直在使用Dispatchers.Main协程范围以外的调度程序,例如viewModelScope.launch(Dispatchers.Default),那么即使您正在使用测试调度程序,您的测试也会再次失败。原因是您只能覆盖主调度程序,Dispatchers.setMain()因为它可以从它的名称中理解,而不是Dispatchers.IOor Dispatchers.Default。在这种情况下,您需要注入mainCoroutineRule.testDispatcher您的视图模型并使用注入的调度程序,而不是对其进行硬编码。

于 2020-06-01T22:15:09.830 回答
1

您面临的问题是因为 toList() 需要流程来完成,并且根据文档“状态流程永远不会完成”。

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/to-list.html

于 2021-01-18T18:15:23.967 回答
0

我们可以为给定创建一个协程,为任何时候创建一个协程

在任何代码之后,我们可以使用yield,这样我们给定的代码就会完成并准备好断言!

在此处输入图像描述

为此,您需要扩展 CouroutinScope,如您所见:

在此处输入图像描述

完毕!

  • 你可以使用 emit 而不是 tryEmit
于 2021-08-24T14:33:54.360 回答