Skip to content

Instantly share code, notes, and snippets.

@joetimmins
Created September 7, 2022 11:18
Show Gist options
  • Save joetimmins/19504c99b6c94f4d1096cdae6858fb58 to your computer and use it in GitHub Desktop.
Save joetimmins/19504c99b6c94f4d1096cdae6858fb58 to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalCoroutinesApi::class)
fun <T : Any> Flow<T>.checkValues(
vararg actions: suspend () -> Unit,
assert: List<T>.() -> Unit,
) = runTest {
// replace main & viewModelScope with the standard test dispatcher
Dispatchers.setMain(StandardTestDispatcher(testScheduler))
val results = mutableListOf<T>()
// collect the flow on an unconfined test dispatcher
val job = this@checkValues
.onEach { results.add(it) }
.launchIn(this + UnconfinedTestDispatcher(testScheduler))
actions.forEach { action ->
action()
// ensure any queued actions are run
advanceUntilIdle()
}
results.assert()
job.cancel()
Dispatchers.resetMain()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment