Comments (4)
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.
from imagine.
@wajahatkarim3 I'm referring to Paging 3.
This is my test:
@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 onChanged(match {
scope.
from imagine.
Thank you for sharing the code snippet. I will add the Paging 3 API and see how it works for the tests.
from imagine.
@wajahatkarim3 Have you found the solution for this?
from imagine.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagine.