如何对从 Paging 3 返回 PagingSource 的 Room Dao 查询进行单元测试 [英] How to Unit Test a Room Dao Query that Returns a PagingSource From Paging 3

查看:80
本文介绍了如何对从 Paging 3 返回 PagingSource 的 Room Dao 查询进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题实际上很笼统.我想知道如何对 Room Dao 查询进行单元测试,该查询从 Paging 3 返回 PagingSource.>

我有一个房间 Dao 查询:

 @Query(SELECT * FROM database")fun getChocolateListData(): PagingSource

我想知道如何对这个查询进行单元测试.

到目前为止我尝试过的(使用内存Room数据库进行测试):

@FlowPreview@测试有趣的 saveChocolateToDbSavesData() = runBlocking {val dao: 通过注入()瓦尔巧克力 = 巧克力(name =鸽子")dao.saveChocolate(巧克力)val pagingSourceFactory = { dao.getChocolateListData() }val pagingDataFlow: Flow<PagingData<Chocolate>>= 寻呼机(配置 = 分页配置(页面大小 = 50,最大尺寸 = 200,enablePlaceholders = false),pagingSourceFactory = pagingSourceFactory).流动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

不知道如何做对.任何帮助将不胜感激!

解决方案

PagingData 是内部事件流的包装器,您无法直接比较它,并且您得到的错误是按预期抛出引用不等式.

相反,您应该直接查询 PagingSource 以比较 LoadResult.Page 中的数据,或者您需要将其连接到演示器 API,例如 AsyncPagingDataDifferPagingDataAdapter 并使用 .snapshot()

val flow = Pager(..).flowval 适配器 = MyPagingDataAdapter()val 作业 = 启动 {flow.collectLatest { adapter.submitData(it) }}//在这里做你的断言工作.取消()

如果你需要一个测试范围,我推荐 kotlinx.coroutines.test 库中的 runBlockingTest

要直接查询PagingSource,它有一个挂起的.load()方法,所以你可以简单地把它包装在runBlockingTest中并断言结果:

@Test有趣的测试()= runBlockingTest {val pagingSource = MyPagingSource()val 实际 = pagingSource.load(LoadParams.Refresh(...))assertEquals(actual as?LoadResult.Page)?.data, listOf(...))}

My question is actually quite generic. I want to know how to unit test a Room Dao query that returns a PagingSource From Paging 3.

I have a Room Dao query:

    @Query("SELECT * FROM database")
    fun getChocolateListData(): PagingSource<Int, Chocolate>

I'm wondering how this query can be unit tested.

What I've tried so far (using in-memory Room database for testing):

@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])
}

This doesn't pass, however:

junit.framework.AssertionFailedError: Expected :androidx.paging.PagingData@7d6c23a1 Actual :androidx.paging.PagingData@321123d2

Not sure how to get it right. Any help would be greatly appreciated!

解决方案

PagingData is wrapper around an internal event stream, you cannot compare it directly and the error you are getting is throwing referential inequality as expected.

Instead you should either query the PagingSource directly to compare the data in LoadResult.Page or you'll need to hook it up to a presenter API such as AsyncPagingDataDiffer or PagingDataAdapter and use .snapshot()

val flow = Pager(..).flow
val adapter = MyPagingDataAdapter()
val job = launch {
    flow.collectLatest { adapter.submitData(it) }
}
// Do your asserts here
job.cancel()

if you need a test scope, I recommend runBlockingTest from the kotlinx.coroutines.test library

To query PagingSource directly, it has a single suspending .load() method, so you can simply wrap it in runBlockingTest and assert the result:

@Test
fun test() = runBlockingTest {
  val pagingSource = MyPagingSource()
  val actual = pagingSource.load(LoadParams.Refresh(...))
  assertEquals(actual as? LoadResult.Page)?.data, listOf(...))
}

这篇关于如何对从 Paging 3 返回 PagingSource 的 Room Dao 查询进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆