Skip to content

Instantly share code, notes, and snippets.

@KwabenBerko
Last active December 8, 2021 19:48
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 KwabenBerko/f6c2670a70556eea90da80dbf2c2b988 to your computer and use it in GitHub Desktop.
Save KwabenBerko/f6c2670a70556eea90da80dbf2c2b988 to your computer and use it in GitHub Desktop.
class MainCoroutineRule : TestWatcher() {
private val testDispatcher = TestCoroutineDispatcher()
override fun starting(description: Description?) {
Dispatchers.setMain(testDispatcher)
}
fun setup(setupBody: () -> Unit){
testDispatcher.pauseDispatcher()
setupBody.invoke()
}
fun run(testBody: () -> Unit){
testDispatcher.resumeDispatcher()
testBody.invoke()
}
override fun finished(description: Description?) {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment