谷歌最近宣布了新的 Paging 3 库、Kotlin-first 库、支持协程和 Flow 等。
我玩过他们提供的代码实验室,但似乎还没有任何测试支持,我还检查了文档。他们没有提到任何关于测试的内容,所以例如我想对这个 PagingSource 进行单元测试:
class GithubPagingSource(private val service: GithubService,
private val query: String) : PagingSource<Int, Repo>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Repo> {
//params.key is null in loading first page in that case we would use constant GITHUB_STARTING_PAGE_INDEX
val position = params.key ?: GITHUB_STARTING_PAGE_INDEX
val apiQuery = query + IN_QUALIFIER
return try {
val response = service.searchRepos(apiQuery, position, params.loadSize)
val data = response.items
LoadResult.Page(
data,
if (position == GITHUB_STARTING_PAGE_INDEX) null else position - 1,
if (data.isEmpty()) null else position + 1)
}catch (IOEx: IOException){
Log.d("GithubPagingSource", "Failed to load pages, IO Exception: ${IOEx.message}")
LoadResult.Error(IOEx)
}catch (httpEx: HttpException){
Log.d("GithubPagingSource", "Failed to load pages, http Exception code: ${httpEx.code()}")
LoadResult.Error(httpEx)
}
}
}
那么,我该如何测试,有人可以帮助我吗?