Last active
December 8, 2021 19:48
-
-
Save KwabenBerko/f6c2670a70556eea90da80dbf2c2b988 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
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