[英]How to test Dao methods which return DataSource.Factory?
After shifting from SqliteOpenHelper
to room
in my app, I've trying to write tests for the DAO
class. 在从我的应用程序中的
SqliteOpenHelper
转移到room
后,我试图为DAO
类编写测试。
My DAO looks something like this: 我的DAO看起来像这样:
@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
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
, ie getItemList
apis. 在为
getAllCards
编写测试时, insertCard
和markRead
是微不足道的,我仍然不确定如何测试返回DataSource.Factory
的api,即getItemList
apis。
After searching on internet, I couldn't find anything related to this. 在互联网上搜索后,我找不到与此相关的任何内容。
Can someone please help. 有人可以请帮助。
this is how I did: 这就是我做的:
val factory = dao.getItemList()
val list = (factory.create() as LimitOffsetDataSource).loadRange(0, 10)
Quoting CommonsWare 引用CommonsWare
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.
如果对Room使用分页并使用@Dao方法返回DataSource.Factory,则生成的代码使用名为LimitOffsetDataSource的内部类来执行SQLite操作并完成PositionalDataSource合同。
source: https://commonsware.com/AndroidArch/previews/paging-beyond-room#head206 来源: https : //commonsware.com/AndroidArch/previews/paging-beyond-room#head206
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.