-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question]: PagingData<ModelName> instead of List<ModelName> #18
Comments
Are you referring to Paging 2 or 3? I tried this with Paging 2 but my tests couldn't pass. So I will change the implementation to Paging 3 in a separate branch and also update the tests as well. |
@wajahatkarim3 I'm referring to Paging 3. @RunWith(JUnit4::class)
class ProductsListViewModelTest {
private lateinit var viewModel: ProductsListViewModel
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@ExperimentalCoroutinesApi
@get:Rule
var coroutineRule = MainCoroutineRule()
@MockK
lateinit var getBeersListUseCase: GetBeersListUseCase
@MockK
lateinit var getBeersListByCoroutineUseCase: GetBeersListByCoroutineUseCase
@MockK
lateinit var savedStateHandle: SavedStateHandle
@Before
fun setUp() {
MockKAnnotations.init(this)
}
@After
fun tearDown() {
}
@ExperimentalCoroutinesApi
@Test
fun `test when ProductsListViewModel is initialized, products are fetched`() =
coroutineRule.testDispatcher.runBlockingTest {
// Given
val givenProducts = ProductUIFactory.createProducts(3)
val flow = flow<PagingData<RecyclerItem>> {
// emit(LoadState.Loading)
delay(10)
emit(PagingData.from(givenProducts))
}
val productsListObserver = mockk<Observer<PagingData<RecyclerItem>>>(relaxed = true)
// When
coEvery {
getBeersListUseCase.invoke(GetBeersListParams(anyString()))
getBeersListByCoroutineUseCase.invoke(GetBeersListByCoroutineParams(anyString()))
}
.returns(flow)
// Invoke
every {
savedStateHandle.get<ChoosePathType>(CHOOSE_PATH_TYPE) ?: ChoosePathType.COROUTINE
} returns ChoosePathType.COROUTINE
viewModel = ProductsListViewModel(
getBeersUseCase = getBeersListUseCase,
getBeersListByCoroutineUseCase = getBeersListByCoroutineUseCase,
savedStateHandle = savedStateHandle
)
viewModel.productsListByCoroutine.asLiveData().observeForever(productsListObserver)
// Then
coroutineRule.advanceTimeBy(10)
coVerify(exactly = 1) {
getBeersListByCoroutineUseCase.invoke(
GetBeersListByCoroutineParams(anyString())
)
}
//verify { productsListObserver.onChanged(match { it == PagingData.from(givenProducts) }) }
verify {
productsListObserver.onChanged(match { it != null })
}
verify(exactly = 1) {
productsListObserver.onChanged(match { it != null })
}
/*val expectedFlow = flow<PagingData<RecyclerItem>> {
emit(PagingData.from(givenProducts))
}*/
// assertNotNull(flow)
// verify { productsListObserver.onChanged(match { it == expectedFlow }) }
}
} But, not sure what to write in the |
Thank you for sharing the code snippet. I will add the Paging 3 API and see how it works for the tests. |
@wajahatkarim3 Have you found the solution for this? |
What if the mocked data is PagingData? What should we write here:
Imagine/app/src/test/java/com/wajahatkarim3/imagine/ui/home/HomeViewModelTest.kt
Line 69 in 6c74f25
The text was updated successfully, but these errors were encountered: