我在一个名为的存储库类中有一个函数getAll()
。我的问题是,我如何测试这个功能?这是函数内部的代码getAll()
。
fun getAll(): Flow<PagingData<Movie>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 20),
pagingSourceFactory = { MoviesPagingSource(movieApiService) }
).flow
}
这是 MoviesPagingSource 类中的内容。
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.oazisn.moviecatalog.data.local.entity.Movie
class MoviesPagingSource(
private val service: MovieApiService,
) : PagingSource<Int, Movie>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Movie> {
val page = params.key ?: 1
return try {
val response = service.getPopular(page)
val movies = ArrayList<Movie>()
response.results?.forEach { data ->
movies.add(
Movie(
"https://image.tmdb.org/t/p/original${data?.posterPath}",
data?.title ?: "-",
data?.voteAverage ?: 0.0,
data?.id ?: 0
)
)
}
LoadResult.Page(
data = movies,
prevKey = if (page == 1) null else page - 1,
nextKey = if (page == response.totalPages) null else page + 1
)
} catch (exception: Exception) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, Movie>): Int? {
// Try to find the page key of the closest page to anchorPosition, from
// either the prevKey or the nextKey, but you need to handle nullability
// here:
// * prevKey == null -> anchorPage is the first page.
// * nextKey == null -> anchorPage is the last page.
// * both prevKey and nextKey null -> anchorPage is the initial page, so
// just return null.
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
}
PagingData
是 Paging v3 android 库中的一个类。https://developer.android.com/topic/libraries/architecture/paging/v3-overview
这是我迄今为止尝试过的单元测试:
@Test
fun getAll() {
testCoroutineRule.runBlockingTest {
val dummyList = listOf(
MovieResultItem(
posterPath = "/6Wdl9N6dL0Hi0T1qJLWSz6gMLbd.jpg",
title = "Mortal Kombat",
voteAverage = 7.9,
id = 460465
),
MovieResultItem(
posterPath = "/pgqgaUx1cJb5oZQQ5v0tNARCeBp.jpg",
title = "Godzilla vs. Kong",
voteAverage = 8.2,
id = 399566
),
MovieResultItem(
posterPath = "/oBgWY00bEFeZ9N25wWVyuQddbAo.jpg",
title = "Nobody",
voteAverage = 8.5,
id = 615457
),
MovieResultItem(
posterPath = "/tnAuB8q5vv7Ax9UAEje5Xi4BXik.jpg",
title = "Zack Snyder's Justice League",
voteAverage = 8.5,
id = 791373
)
)
val dummyObject = ListBaseResponse(
results = dummyList
)
`when`(movieApiService.getPopular(1)).thenReturn(dummyObject)
val movies = ArrayList<Movie>()
dummyObject.results?.forEach { data ->
movies.add(
Movie(
"https://image.tmdb.org/t/p/original${data?.posterPath}",
data?.title ?: "-",
data?.voteAverage ?: 0.0,
data?.id ?: 0
)
)
}
val response = repository.getAll()
val repoResponseAsLiveData = response.asLiveData()
assertEquals(movies, repoResponseAsLiveData.value)
}
}
结果是
java.lang.AssertionError:
Expected :[Movie(poster=https://image.tmdb.org/t/p/original/6Wdl9N6dL0Hi0T1qJLWSz6gMLbd.jpg, title=Mortal Kombat, rating=7.9, id=460465, rated=, duration=, genre=, releaseDate=, desc=, director=, writers=, stars=, creator=), Movie(poster=https://image.tmdb.org/t/p/original/pgqgaUx1cJb5oZQQ5v0tNARCeBp.jpg, title=Godzilla vs. Kong, rating=8.2, id=399566, rated=, duration=, genre=, releaseDate=, desc=, director=, writers=, stars=, creator=), Movie(poster=https://image.tmdb.org/t/p/original/oBgWY00bEFeZ9N25wWVyuQddbAo.jpg, title=Nobody, rating=8.5, id=615457, rated=, duration=, genre=, releaseDate=, desc=, director=, writers=, stars=, creator=), Movie(poster=https://image.tmdb.org/t/p/original/tnAuB8q5vv7Ax9UAEje5Xi4BXik.jpg, title=Zack Snyder's Justice League, rating=8.5, id=791373, rated=, duration=, genre=, releaseDate=, desc=, director=, writers=, stars=, creator=)]
Actual :null