Manuel Lorenzo
10/22/2022, 6:49 PMclass RepositoriesRepositoryImpl @Inject constructor(
@IoCoroutineDispatcher private val ioCoroutineDispatcher: CoroutineDispatcher,
private val service: GithubService,
private val database: RepositoriesDatabase
) : RepositoriesRepository {
@OptIn(ExperimentalPagingApi::class)
override suspend fun fetchRepositories() = withContext(ioCoroutineDispatcher) {
return@withContext Pager(config = PagingConfig(
pageSize = NETWORK_PAGE_SIZE, enablePlaceholders = false
),
remoteMediator = GithubRemoteMediator(ioCoroutineDispatcher, service, database),
pagingSourceFactory = { database.repositoriesDao().allRepositories() }).flow
}
companion object {
const val NETWORK_PAGE_SIZE = 10
}
}
And this is my test class:
@ExperimentalCoroutinesApi
class RepositoriesRepositoryTest {
private lateinit var sut: RepositoriesRepository
private val testDispatcher = UnconfinedTestDispatcher()
private val fakeGithubService: GithubService = mock {
onBlocking { fetchRepos(any(), any()) } doSuspendableAnswer {
Response.success((listOf()))
}
}
private val inMemoryDatabase = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(), RepositoriesDatabase::class.java
).build()
@Before
fun setup() {
sut = RepositoriesRepositoryImpl(testDispatcher, fakeGithubService, inMemoryDatabase)
}
@Test
fun test1() = runTest {
sut.fetchRepositories().test {
val item: PagingData<Repository> = awaitItem()
assertNull(item)
}
}
}
What I’m trying is to assert the content of the PagingData
, just to see that it contains whatever number repository I’m passing in the test expectation.
How could I do this?
Thanks!