- Notifications
You must be signed in to change notification settings - Fork 0
Boiler plate code to make threading work on the coroutines in the mockK
Devrath edited this page Jun 26, 2021 · 2 revisions
@ExperimentalCoroutinesApi @get:Rule val coroutineTestRule = CoroutinesTestRule() @get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule()/** * Sets the main coroutines dispatcher for unit testing. * * See https://medium.com/androiddevelopers/easy-coroutines-in-android-viewmodelscope-25bffb605471 * and https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-test */ @ExperimentalCoroutinesApi class CoroutinesTestRule( 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() } }