Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Created January 12, 2020 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RBusarow/81b38ec01904d64500137bfccad30d62 to your computer and use it in GitHub Desktop.
Save RBusarow/81b38ec01904d64500137bfccad30d62 to your computer and use it in GitHub Desktop.
class CoroutineTestRule : TestRule,
TestPolymorphicCoroutineScope by TestPolymorphicCoroutineScope() {
val dispatcher = coroutineContext[ContinuationInterceptor] as TestCoroutineDispatcher
override fun apply(
base: Statement, description: Description?
) = object : Statement() {
override fun evaluate() {
@Throws(Throwable::class)
override fun evaluate() {
Dispatchers.setMain(dispatcher)
base.evaluate()
cleanupTestCoroutines()
Dispatchers.resetMain()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment