Skip to content

Instantly share code, notes, and snippets.

@CostaFot
Last active January 19, 2020 21:25
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 CostaFot/b690f92be527c899f8321d434804430e to your computer and use it in GitHub Desktop.
Save CostaFot/b690f92be527c899f8321d434804430e to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
class CoroutineTestRule(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
val testDispatcherProvider = object : DispatcherProvider {
override val io: CoroutineDispatcher = testDispatcher
override val ui: CoroutineDispatcher = testDispatcher
override val default: CoroutineDispatcher = testDispatcher
override val unconfined: CoroutineDispatcher = testDispatcher
}
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment