我的问题实际上很笼统。我想知道如何对从Paging 3返回PagingSource的Room Dao查询进行单元测试。
我有一个Room Dao查询:
@Query("SELECT * FROM database")
fun getChocolateListData(): PagingSource<Int, Chocolate>
我想知道如何对这个查询进行单元测试。
到目前为止我尝试过的(使用内存中的Room数据库进行测试):
@FlowPreview
@Test
fun saveChocolateToDbSavesData() = runBlocking {
val dao: Dao by inject()
val chocolate = Chocolate(
name = "Dove"
)
dao.saveChocolate(chocolate)
val pagingSourceFactory = { dao.getChocolateListData() }
val pagingDataFlow: Flow<PagingData<Chocolate>> = Pager(
config = PagingConfig(
pageSize = 50,
maxSize = 200,
enablePlaceholders = false
),
pagingSourceFactory = pagingSourceFactory
).flow
val chocolateListFlow = pagingDataFlow.testIn(coroutinesTestRule)
Assert.assertEquals(PagingData.from(listOf(chocolate)), chocolateListFlow.emissions[0])
}
然而,这并没有通过:
junit.framework.AssertionFailedError:预期:androidx.paging.PagingData@7d6c23a1 实际:androidx.paging.PagingData@321123d2
不知道如何正确处理。任何帮助将不胜感激!