Android单位测试用于EMITALL的流量

发布于 2025-02-07 00:48:27 字数 1138 浏览 2 评论 0 原文

我的功能可以通过emitall返回流动

 fun handle(actions: MoviesActions): Flow<MoviesStates> = flow {
    when (actions) {
        is MoviesActions.LoadMovies -> {
            emit(MoviesStates.Loading)
            emitAll(moviesUseCase.execute())
        }
    }
}

,并且用例函数函数

suspend fun execute(): Flow<MoviesStates> = flow {
    combine(f1, f2) { state1: MoviesStates, state2: MoviesStates ->
     
          // some code
        
    }.collect {
        emit(it)
    }
}

在测试第一个发射 moviesstates.loading 时毫无问题,问题是我尝试测试Emitall 代码> emitall(Movieusecase.execute()),测试失败,我得到了此结果

java.util.nosuchelementException:预期至少一个元素

这是我的单位测试

@Test
fun testLoadMovies() = runBlocking {
    whenever(useCase.execute()).thenReturn(flow {
        MoviesStates.EmptyList
    })

    val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
    val expected = MoviesStates.EmptyList

    assertEquals(actual, expected)
}

,我该如何正确测试?

I have a function that return flow by emitAll

 fun handle(actions: MoviesActions): Flow<MoviesStates> = flow {
    when (actions) {
        is MoviesActions.LoadMovies -> {
            emit(MoviesStates.Loading)
            emitAll(moviesUseCase.execute())
        }
    }
}

And this the use case function

suspend fun execute(): Flow<MoviesStates> = flow {
    combine(f1, f2) { state1: MoviesStates, state2: MoviesStates ->
     
          // some code
        
    }.collect {
        emit(it)
    }
}

No problem in testing the first emission MoviesStates.Loading, the problem is when I try to test the flow which return from usecase by emitAll emitAll(moviesUseCase.execute()), the test fails and I got this result

java.util.NoSuchElementException: Expected at least one element

this is my unit test

@Test
fun testLoadMovies() = runBlocking {
    whenever(useCase.execute()).thenReturn(flow {
        MoviesStates.EmptyList
    })

    val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
    val expected = MoviesStates.EmptyList

    assertEquals(actual, expected)
}

So How can I test it correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-02-14 00:48:27

感谢 gpunto ,这是他建议的解决方案

@Test
fun testLoadMovies() = runTest {
    whenever(useCase.execute()).thenReturn(flow {
         MoviesStates.EmptyList
    })

    useCase.execute().collectLatest { states ->
        val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
        val expected = states 
        assertEquals(expected, actual)
    }
}

Thanks to gpunto , this is the solution he suggested

@Test
fun testLoadMovies() = runTest {
    whenever(useCase.execute()).thenReturn(flow {
         MoviesStates.EmptyList
    })

    useCase.execute().collectLatest { states ->
        val actual = viewModel.handle(MoviesActions.LoadMovies).drop(1).first()
        val expected = states 
        assertEquals(expected, actual)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文