android - 在 Android 单元测试中检查 PagingData 对象数据的正确方法是什么

标签 android unit-testing android-paging

我正在使用分页库从 api 检索数据并将它们显示在列表中
为此,我在我的存储库中创建了该方法:fun getArticleList(query: String): Flow<PagingData<ArticleHeaderData>>在我的 View 模型中,我创建了类似这样的搜索方法:

override fun search(query: String) {
    val lastResult = articleFlow
    if (query == lastQuery && lastResult != null)
        return
    lastQuery = query
    searchJob?.cancel()
    searchJob = launch {
        val newResult: Flow<PagingData<ArticleList>> = repo.getArticleList(query)
            .map {
                it.insertSeparators { //code to add separators }.cachedIn(this)
        articleFlow = newResult
        newResult.collectLatest {
            articleList.postValue(it)
        }
    }
}
为了测试我的 View 模型,我使用了测试方法 PagingData.from创建一个流以从我的模拟存储库返回,如下所示:whenever(repo.getArticleList(query)).thenReturn(flowOf(PagingData.from(articles)))然后我从 articleList LiveData 中检索实际的分页数据,如下所示:val data = vm.articleList.value!!这将返回 PagingData<ArticleList>我想验证的对象是否包含来自服务的数据(即 articles 无论何时返回)
我发现这样做的唯一方法是创建以下扩展函数:
private val dcb = object : DifferCallback {
    override fun onChanged(position: Int, count: Int) {}
    override fun onInserted(position: Int, count: Int) {}
    override fun onRemoved(position: Int, count: Int) {}
}

suspend fun <T : Any> PagingData<T>.collectData(): List<T> {
    val items = mutableListOf<T>()
    val dif = object : PagingDataDiffer<T>(dcb, TestDispatchers.Immediate) {
        override suspend fun presentNewList(previousList: NullPaddedList<T>, newList: NullPaddedList<T>, newCombinedLoadStates: CombinedLoadStates, lastAccessedIndex: Int): Int? {
            for (idx in 0 until newList.size)
                items.add(newList.getFromStorage(idx))
            return null
        }
    }
    dif.collectFrom(this)
    return items
}
这似乎可行,但基于 PagingDataDiffer标记为 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 的类所以它可能在 future 不起作用
有没有更好的方法来获得 flow从 PagingData (在库中标记为内部)或从中获取实际数据?

最佳答案

我对 Paging3 库也有同样的问题,网上还没有很多关于这个库的讨论,但是当我浏览一些文档时,我可能会找到一个解决方案。我面临的情况是试图确定 PagingData 中的数据列表是否为空。 ,然后我将在此基础上操作 UI。
这是我在文档中找到的,PagingDataAdapter 中有两个 api已在版本 3.0.0-alpha04 中添加这是peek() , 和 snapshot() , peek()给我们一个基于索引的特定列表对象,而 snapshot()给了我们整个 list 。
所以这就是我所做的:

lifecycleScope.launch {
    //Your PagingData flow submits the data to your RecyclerView adapter
    viewModel.allConversations.collectLatest {
        adapter.submitData(it)
    }
}
lifecycleScope.launch {
    //Your adapter's loadStateFlow here
    adapter.loadStateFlow.
        distinctUntilChangedBy {
            it.refresh
        }.collect {
            //you get all the data here
            val list = adapter.snapshot()
            ...
        }
    }
因为我刚刚接触到 Paging 库和 Flow最近,这种方法可能存在缺陷,如果有更好的方法,请告诉我!

关于android - 在 Android 单元测试中检查 PagingData 对象数据的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63522656/

相关文章:

iphone - 跨平台移动应用程序开发框架如何工作?

java - 提高文件搜索和 MediadataRetriever 的性能

python - 将 float 组转换为小/大端

android - 在 PagedList 获取新数据时收到通知

kotlin - PageKeyedDataSource loadAfter 连续调用

android - TestNG结果的testng-results.xml中的 "duration in ms"代表什么?

java - 如何使用 JSOUP POST 表单登录?

unit-testing - 单元测试应该模拟所有外部服务吗?

ios - 在单元测试用例 (RestKit) 之间重置持久性存储的零星崩溃

android - PageList.size 始终为零