Vipul
07/22/2020, 5:41 PMremoteCloudDataStore.getFoodItems()
and localDataStore.addFoodItems(it)
methods are called exactly once
fun getFoodItems(forceNetworkFetch: Boolean) = remoteCloudDataStore.getFoodItems().flatMapConcat {
localDataStore.addFoodItems(it).flatMapConcat {
flow { emit(FoodResult.Success(it)) }
}
}.flowOn(<http://dispatcher.io|dispatcher.io>)
Below is my test case
repository.getFoodItems(true).collect {
verify(exactly = 1) { remoteCloudDataStore.getFoodItems() }
verify(exactly = 5) { localDataStore.addFoodItems(any()) }
}
Surprisingly this test case pass, even if addFoodItems() method is not being called 5 times, can someone point me in correct direction what I am doing wrong here? I am passing TestCoroutineDispatcher
during testing.Luis Munoz
07/22/2020, 5:48 PMJustin Tullgren
07/22/2020, 5:57 PMVipul
07/22/2020, 6:00 PMlocalDataStore.addFoodItems
is not being called 5 times in method body.toList()
I receive:
io.mockk.MockKException: Missing calls inside verify { ... } block.
Justin Tullgren
07/22/2020, 6:04 PMLuis Munoz
07/22/2020, 6:14 PMremoteCloudDataStore.getFoodItems() returns a flow
verify(exactly = 1) { remoteCloudDataStore.getFoodItems().toList() }
Vipul
07/22/2020, 6:16 PMhow many items
test case is planned for sure 🙂Luis Munoz
07/22/2020, 6:17 PMVipul
07/22/2020, 6:20 PMrepository.getFoodItems(true).collect {
coVerify(exactly = 1) { remoteCloudDataStore.getFoodItems().toList() }
coVerify(exactly = 5) { localDataStore.getFoodItems().toList() }
}
Luis Munoz
07/22/2020, 6:24 PMassert localDataStore.getFoodItems().toList().size is equal to 5
Justin Tullgren
07/22/2020, 6:25 PMval items = repository.getFoodItems(true).toList()
verify(exactly = 1) { remoteCloudDataStore.getFoodItems() }
Luis Munoz
07/22/2020, 6:25 PMJustin Tullgren
07/22/2020, 6:26 PMVipul
07/22/2020, 7:32 PMrepository.getFoodItems(true).collect {
verify(exactly = 11) { remoteCloudDataStore.getFoodItems() }
}
Luis Munoz
07/22/2020, 7:42 PM@Test
fun `force fetching foodItems makes network and db call`() = {
val latch = CountDownLatch(1)
dispatcher.runBlockingTest {
coEvery { restApi.getFoodItems() } returns MockData.foodItems
every { localDataStore.addFoodItems(any()) } returns flow { MockData.foodItem }
repository.getFoodItems(true).collect {
verify(exactly = 11) { remoteCloudDataStore.getFoodItems() }
latch.countDown()
}
}
latch.await(3, TimeUnit.Seconds)
}
Justin Tullgren
07/22/2020, 7:58 PMval items = repository.getFoodItems(true).toList()
verify(exactly = 1) { remoteCloudDataStore.getFoodItems() }
Vipul
07/22/2020, 7:59 PMJustin Tullgren
07/22/2020, 7:59 PMcoEvery { restApi.getFoodItems() } returns MockData.foodItems
every { localDataStore.addFoodItems(any()) } returns flow { MockData.foodItem }
repository.getFoodItems(true).toList()
verify(exactly = 11) { remoteCloudDataStore.getFoodItems() }
Vipul
07/22/2020, 8:02 PMrepository.getFoodItems(true).toList()
verify(exactly = 11) { remoteCloudDataStore.getFoodItems() }
Luis Munoz
07/22/2020, 8:02 PMremoteCloudDataStore.getFoodItems().flatMapConcat
Vipul
07/22/2020, 8:03 PMrestApi.getFoodItems()
levelremoteCloudDataStore = CloudCloudDataStoreImpl(restApi)
restApi.getFoodItems()
Luis Munoz
07/22/2020, 8:04 PM@Test
fun `force fetching foodItems makes network and db call`() =
dispatcher.runBlockingTest {
coEvery { restApi.getFoodItems() } returns MockData.foodItems
every { localDataStore.addFoodItems(any()) } returns flow { MockData.foodItem }
repository.getFoodItems(true).collect {
throw RuntimeException("test should fail")
verify(exactly = 11) { remoteCloudDataStore.getFoodItems() }
}
}
@Test
fun `force fetching foodItems makes network and db call`() =
dispatcher.runBlockingTest {
coEvery { restApi.getFoodItems() } returns MockData.foodItems
every { localDataStore.addFoodItems(any()) } returns flow { MockData.foodItem }
repository.getFoodItems(true).toList()
// throw RuntimeException("test should fail")
coVerify(exactly = 11) { remoteCloudDataStore.getFoodItems().toList() }
}
override fun getFoodItems(forceNetworkFetch: Boolean): Flow<FoodResult<List<FoodItem>>>
Vipul
07/22/2020, 9:10 PM