Created
June 15, 2020 20:14
-
-
Save cdmunoz/885906d35ef2d21521ad8561ab21725d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@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