2021年1月26日星期二

How To Test PagingData From Paging 3

My ViewModel has a method which returns a flow of PagingData. In my app, the data is fetched from the remote server, which is then saved to Room (the single source of truth):

fun getChocolates(): Flow<PagingData<Chocolate>> {      val pagingSourceFactory = { dao().getChocolateListData() }      return Pager(          config = PagingConfig(              pageSize = NETWORK_PAGE_SIZE,              maxSize = MAX_MEMORY_SIZE,              enablePlaceholders = false          ),          remoteMediator = ChocolateRemoteMediator(                  api,                  dao          ),          pagingSourceFactory = pagingSourceFactory      ).flow  }  

How do I test this method? I want to test if the returning flow contains the correct data.

What I've tried so far:

@InternalCoroutinesApi  @Test  fun getChocolateListReturnsCorrectData() = runBlockingTest {      val dao: Dao by inject()      val viewModel: ViewModel by inject()        // 1      val chocolate1 = Chocolate(          name = "Dove"      )      val chocolate2 = Chocolate(          name = "Hershey's"      )      // 2      dao.saveChocolate(chocolate1, chocolate2)      // 3      val pagingSourceFactory = { dao.getChocolateListData() }      val pagingData = Pager(          config = PagingConfig(              pageSize = 50,              maxSize = 200,              enablePlaceholders = false          ),          pagingSourceFactory = pagingSourceFactory      ).flow.collectLatest {        }      viewModel.getChocolates().collectLatest {        }      // 4    }  

I'm wondering if I'm on the right track. Any help would be greatly appreciated!

https://stackoverflow.com/questions/65810968/how-to-test-pagingdata-from-paging-3 January 20, 2021 at 09:56PM

没有评论:

发表评论