繁体   English   中英

协程单元测试在该类中失败

[英]Coroutine Unit Testing Fails for the Class

我在对协程进行单元测试时遇到一个奇怪的问题。 该类上有两个测试,分别运行时,它们都会通过;当我运行完整的测试类时,一个测试会因断言error而失败 我正在使用MainCoroutineRule来使用TestCoroutineScope并依赖于最新的Coroutine测试库这是测试:

    @Test
    fun testHomeIsLoadedWithShowsAndFavorites() {
        runBlocking {
            // Stubbing network and repository calls
            whenever(tvMazeApi.getCurrentSchedule("US", currentDate))
                .thenReturn(getFakeEpisodeList())
            whenever(favoriteShowsRepository.allFavoriteShowIds())
                .thenReturn(arrayListOf(1, 2))
        }

        mainCoroutineRule.runBlockingTest {
            // call home viewmodel
            homeViewModel.onScreenCreated()
            // Check if loader is shown
            assertThat(LiveDataTestUtil.getValue(homeViewModel.getHomeViewState())).isEqualTo(Loading)
            // Observe on home view state live data
            val homeViewState = LiveDataTestUtil.getValue(homeViewModel.getHomeViewState())
            // Check for success data
            assertThat(homeViewState is Success).isTrue()
            val homeViewData = (homeViewState as Success).homeViewData
            assertThat(homeViewData.episodes).isNotEmpty()
            // compare the response with fake list
            assertThat(homeViewData.episodes).hasSize(getFakeEpisodeList().size)
            // compare the data and also order
            assertThat(homeViewData.episodes).containsExactlyElementsIn(getFakeEpisodeViewDataList(true)).inOrder()
        }
    }

另一个测试几乎与没有收藏夹的节目测试类似。 我正在尝试将HomeViewModel方法测试为:

        homeViewStateLiveData.value = Loading
        val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception ->
            onError(exception)
        }

        viewModelScope.launch(coroutineExceptionHandler) {
            // Get shows from network and favorites from room db on background thread
            val favoriteShowsWithFavorites = withContext(Dispatchers.IO) {
                val favoriteShowIds = favoriteShowsRepository.allFavoriteShowIds()
                val episodes = tvMazeApi.getCurrentSchedule(COUNTRY_US, currentDate)
                getShowsWithFavorites(episodes, favoriteShowIds)
            }
            // Return the combined result on main thread
            withContext(Dispatchers.Main) {
                onSuccess(favoriteShowsWithFavorites)
            }
        }
    }

我无法找到为什么如果单独运行测试通过以及当测试完整类时测试之一失败的真正原因。 如果我缺少某些东西,请帮忙

Coroutine随附的Retrofit和Room支持挂起功能,并自行将其移出UI线程。 因此,它们大大减少了开发人员处理线程回调的麻烦。 最初,我通过Dispatchers.IO明确地将网络和数据库的挂起调用移至IO。 这是不必要的,并且还会导致不必要的上下文切换,从而导致不稳定的测试。 由于这些库会自动执行此操作,因此仅在可用时才将数据处理回UI上。

        viewModelScope.launch(coroutineExceptionHandler) {
            // Get favorite shows from db, suspend function in room will launch a new coroutine with IO dispatcher
            val favoriteShowIds = favoriteShowsRepository.allFavoriteShowIds()
            // Get shows from network, suspend function in retrofit will launch a new coroutine with IO dispatcher
            val episodes = tvMazeApi.getCurrentSchedule(COUNTRY_US, currentDate)

            // Return the result on main thread via Dispatchers.Main
            homeViewStateLiveData.value = Success(HomeViewData(getShowsWithFavorites(episodes, favoriteShowIds)))
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM