Lucca Beurmann
12/22/2021, 3:00 PM@Test
fun `Should get images and add to list`() = runBlockingTest {
coEvery { getPhotosUseCase(any()) } returns flow { emit(Source.Success(GetImagesTestData.DOMAIN_RESPONSE)) }
//viewModel.getImages()
coVerify(exactly = 1) { getPhotosUseCase(any()) }
confirmVerified(getPhotosUseCase)
assertEquals(GetImagesTestData.DOMAIN_RESPONSE, viewModel.photos.toList())
}
This test keeps failing because it says that viewModel.photos.ToList() is empty, therefore not equal to the expected result. The test will only pass if call the viewModel.getImages() method inside the test block, but this method is already being called in the init{} block of the viewModel.
Can anyone help me understand why this is happening?Joseph Hawkes-Cates
12/22/2021, 6:12 PMLucca Beurmann
12/22/2021, 6:14 PMLucca Beurmann
12/22/2021, 6:16 PMLucca Beurmann
12/22/2021, 6:16 PMinit {
this.getImages()
}
fun getImages() {
viewModelScope.launch { callUseCase() ....}
}Joseph Hawkes-Cates
12/22/2021, 6:23 PMJoseph Hawkes-Cates
12/22/2021, 6:25 PMLucca Beurmann
12/22/2021, 6:34 PMLucca Beurmann
12/22/2021, 6:34 PM@Test
fun `Should set viewstate to error if get images succeeds but returns a null object`() =
runBlocking {
coEvery { getPhotosUseCase(any()) } returns flow { emit(Source.Success(null)) }
viewModel = PhotoListViewModel(getPhotosUseCase, ratePhotosUseCase)
coVerify(exactly = 1) { getPhotosUseCase(1) }
confirmVerified(getPhotosUseCase)
assertTrue(viewModel.screenState.value is ViewState.Error)
}
sorianog
12/23/2021, 2:08 AMprintln()
debugging to get a better sense of what is going on in your setup.