Last active
June 16, 2020 19:19
-
-
Save diefferson/aa0ab816062aec0959b824834b7287ea to your computer and use it in GitHub Desktop.
MainCoroutineRule
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 MainCoroutineRule( | |
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | |
) : TestWatcher() { | |
override fun starting(description: Description?) { | |
super.starting(description) | |
Dispatchers.setMain(testDispatcher) | |
} | |
override fun finished(description: Description?) { | |
super.finished(description) | |
Dispatchers.resetMain() | |
testDispatcher.cleanupTestCoroutines() | |
} | |
fun runBlockingTest( | |
block: suspend TestCoroutineScope.() -> Unit | |
): Unit = testDispatcher.runBlockingTest(block) | |
} |
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 | |
@RunWith(AndroidJUnit4::class) | |
class UseCaseTest { | |
@get:Rule | |
val rule = MainCoroutineRule() | |
lateinit var useCase: UseCase | |
@Before | |
fun setup() { | |
useCase = UseCase( | |
dispatcher = rule.testDispatcher | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment