Skip to content

Instantly share code, notes, and snippets.

@pranay1494
Created May 2, 2020 15:53
Show Gist options
  • Save pranay1494/053f40a3a8c307479a81bdaea2300b57 to your computer and use it in GitHub Desktop.
Save pranay1494/053f40a3a8c307479a81bdaea2300b57 to your computer and use it in GitHub Desktop.
package olx.com.delorean.view.filter.viewmodel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import kotlin.coroutines.ContinuationInterceptor
class CoroutineDispatcherRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
val testDispatcher = TestCoroutineDispatcher()
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment