Skip to content

Instantly share code, notes, and snippets.

@diefferson
Last active June 16, 2020 19:19
Show Gist options
  • Save diefferson/aa0ab816062aec0959b824834b7287ea to your computer and use it in GitHub Desktop.
Save diefferson/aa0ab816062aec0959b824834b7287ea to your computer and use it in GitHub Desktop.
MainCoroutineRule
@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)
}
@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