Created
September 7, 2022 11:18
-
-
Save joetimmins/19504c99b6c94f4d1096cdae6858fb58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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