https://kotlinlang.org logo
#android
Title
# android
a

ashwini

07/28/2019, 8:08 AM
I am facing a weird issue while unit testing Coroutines. There are two tests on the class, when run individually, they both pass and when I run the complete test class, one fails with assertion error. I am using
MainCoroutineRule
to use the
TestCoroutineScope
and relying on the latest Coroutine Testing Library Here is the test :
Copy code
@ExperimentalCoroutinesApi
    @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()
        }
    }
The other test is almost similar which tests for Shows without favorites. I am trying to test
HomeViewModel
method as:
Copy code
fun onScreenCreated() {
        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(<http://Dispatchers.IO|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)
            }
        }
    }
I cannot find the actual cause of why the tests if run separatly are passing and when the complete class is tested, one of them is failing. Pls help if I am missing something
l

louiscad

07/28/2019, 12:59 PM
@ashwini Please, avoid cross-posting as per the rules of this Slack that you should read BTW (you already posted this in #coroutines here: https://kotlinlang.slack.com/archives/C1CFAFJSK/p1564301400159000)
👍 1
r

rkeazor

07/28/2019, 3:22 PM
also avoid posting whole code blocks. We are not code reviewers😂. Just asking a general question is probably good enough
👍 1