如何测试返回DataSource.Factory的Dao方法? [英] How to test Dao methods which return DataSource.Factory?

查看:75
本文介绍了如何测试返回DataSource.Factory的Dao方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中从 SqliteOpenHelper 转移到 room 之后,我试图为 DAO 类编写测试.

After shifting from SqliteOpenHelper to room in my app, I've trying to write tests for the DAO class.

我的DAO看起来像这样:

My DAO looks something like this:

    @Query("SELECT * FROM cards")
    fun getAllCards(): List<CardData>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertCard(vararg cardData: CardData): List<Long>

    @Query("SELECT * FROM cards ORDER BY isRead ASC, id DESC")
    fun getItemList(): DataSource.Factory<Int, CardData>

    @Query("SELECT * FROM cards where instr(title, :query) > 0 ORDER BY isRead ASC, id DESC")
    fun getItemList(query: String): DataSource.Factory<Int, CardData>

    @Query("UPDATE cards set isRead = 1 where title = :title")
    fun markRead(title: String): Int

虽然为 getAllCards insertCard markRead 编写测试很简单,但我仍然不确定如何测试返回 DataSource.Factory ,即 getItemList API.

While writing test for getAllCards, insertCard and markRead is trivial, I am still not sure how do I test the apis which return DataSource.Factory, i.e getItemList apis.

在互联网上搜索后,我找不到与此相关的任何内容.

After searching on internet, I couldn't find anything related to this.

有人可以帮忙吗?

推荐答案

这就是我的做法:

val factory = dao.getItemList()
val list = (factory.create() as LimitOffsetDataSource).loadRange(0, 10)

引用CommonsWare

Quoting CommonsWare

如果您使用Room进行分页并且有一个@Dao方法返回一个DataSource.Factory,则生成的代码将使用一个名为LimitOffsetDataSource的内部类来执行SQLite操作并履行PositionalDataSource合同.

If you use paging with Room and have a @Dao method return a DataSource.Factory, the generated code uses an internal class named LimitOffsetDataSource to perform the SQLite operations and fulfill the PositionalDataSource contract.

来源: https://commonsware.com/AndroidArch/previews/paging-beyond-room#head206

这篇关于如何测试返回DataSource.Factory的Dao方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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