Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created June 15, 2020 20:14
Show Gist options
  • Save cdmunoz/885906d35ef2d21521ad8561ab21725d to your computer and use it in GitHub Desktop.
Save cdmunoz/885906d35ef2d21521ad8561ab21725d to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
class TestCoroutineRule : TestRule {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
override fun apply(base: Statement?, description: Description?) = object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
Dispatchers.setMain(testCoroutineDispatcher)
base?.evaluate()
Dispatchers.resetMain()
testCoroutineScope.cleanupTestCoroutines()
}
}
fun runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) =
testCoroutineScope.runBlockingTest { block() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment