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 PMLuis 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.Vipul
07/22/2020, 6:02 PMtoList()
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
Luis Munoz
07/22/2020, 6:14 PMverify(exactly = 1) { remoteCloudDataStore.getFoodItems().toList() }
Luis Munoz
07/22/2020, 6:15 PMVipul
07/22/2020, 6:16 PMVipul
07/22/2020, 6:16 PMhow many items
test case is planned for sure 🙂Luis Munoz
07/22/2020, 6:17 PMLuis Munoz
07/22/2020, 6:18 PMLuis Munoz
07/22/2020, 6:18 PMVipul
07/22/2020, 6:20 PMrepository.getFoodItems(true).collect {
coVerify(exactly = 1) { remoteCloudDataStore.getFoodItems().toList() }
coVerify(exactly = 5) { localDataStore.getFoodItems().toList() }
}
Vipul
07/22/2020, 6:20 PMVipul
07/22/2020, 6:20 PMLuis Munoz
07/22/2020, 6:24 PMLuis 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 PMLuis Munoz
07/22/2020, 6:25 PMJustin Tullgren
07/22/2020, 6:26 PMVipul
07/22/2020, 7:32 PMVipul
07/22/2020, 7:35 PMVipul
07/22/2020, 7:41 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)
}
Luis Munoz
07/22/2020, 7:44 PMLuis Munoz
07/22/2020, 7:55 PMLuis Munoz
07/22/2020, 7:57 PMJustin Tullgren
07/22/2020, 7:58 PMval items = repository.getFoodItems(true).toList()
verify(exactly = 1) { remoteCloudDataStore.getFoodItems() }
Justin Tullgren
07/22/2020, 7:58 PMVipul
07/22/2020, 7:59 PMVipul
07/22/2020, 7:59 PMJustin Tullgren
07/22/2020, 7:59 PMJustin Tullgren
07/22/2020, 8:00 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 PMLuis Munoz
07/22/2020, 8:02 PMremoteCloudDataStore.getFoodItems().flatMapConcat
Luis Munoz
07/22/2020, 8:03 PMLuis Munoz
07/22/2020, 8:03 PMVipul
07/22/2020, 8:03 PMrestApi.getFoodItems()
levelVipul
07/22/2020, 8:03 PMremoteCloudDataStore = CloudCloudDataStoreImpl(restApi)
Vipul
07/22/2020, 8:03 PMrestApi.getFoodItems()
Luis Munoz
07/22/2020, 8:04 PMLuis Munoz
07/22/2020, 8:26 PMLuis Munoz
07/22/2020, 8:26 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() }
}
}
Luis Munoz
07/22/2020, 8:26 PMLuis Munoz
07/22/2020, 8:28 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).toList()
// throw RuntimeException("test should fail")
coVerify(exactly = 11) { remoteCloudDataStore.getFoodItems().toList() }
}
Luis Munoz
07/22/2020, 8:28 PMLuis Munoz
07/22/2020, 8:34 PMoverride fun getFoodItems(forceNetworkFetch: Boolean): Flow<FoodResult<List<FoodItem>>>
Luis Munoz
07/22/2020, 8:34 PMLuis Munoz
07/22/2020, 8:37 PMVipul
07/22/2020, 9:10 PMVipul
07/22/2020, 9:10 PMVipul
07/22/2020, 9:11 PM