Imagine my ViewModel looks like this :
class ViewModelA(
repository: Repository,
ioCoroutineDispatcher: CoroutineDispatcher,
) : ViewModel() {
val liveData : LiveData<String> = repository.getFooBarFlow().asLiveData(ioCoroutineDispatcher)
}
Imagine my Repository implementation looks like this :
class RepositoryImpl : Repository() {
override fun getFooBarFlow() : Flow<String> = flow {
emit("foo")
delay(300)
emit("bar")
}
}
How could I unit test the fact that "foo" is emitted immediately by the LiveData, then 300ms later (no more, no less), that "bar" is emitted by the LiveData ?
You can use JUnit 4 or 5, but you have to use Kotlin 1.6, kotlinx-coroutines-test 1.6 and runTest {} instead of runBlockingTest {} (I have no issues testing with runBlockingTest {})