dispatch-android-espresso
Tools to provide Espresso functionality for coroutines.
If an IdlingDispatcherProvider is registered with the IdlingRegistry, Espresso will wait for all associated coroutines to leave the active state before performing any assertions.
Coroutines which are in a suspended state (such as a Job "observing" a Flow for updates) do not prevent Espresso from performing assertions.
class IdlingCoroutineScopeRuleWithLifecycleSample {
// Retrieve the DispatcherProvider from a dependency graph,
// so that the same one is used throughout the codebase.
val customDispatcherProvider = testAppComponent.customDispatcherProvider
@JvmField
@Rule
val idlingRule = IdlingDispatcherProviderRule {
IdlingDispatcherProvider(customDispatcherProvider)
}
/**
* If you don't provide CoroutineScopes to your lifecycle components via a dependency injection framework,
* you need to use the `dispatch-android-lifecycle-extensions` and `dispatch-android-viewmodel` artifacts
* to ensure that the same `IdlingDispatcherProvider` is used.
*/
@Before
fun setUp() {
LifecycleScopeFactory.set {
MainImmediateCoroutineScope(customDispatcherProvider)
}
ViewModelScopeFactory.set {
MainImmediateCoroutineScope(customDispatcherProvider)
}
}
@Test
fun testThings() = runBlocking {
// Now any CoroutineScope which uses the DispatcherProvider
// in TestAppComponent will sync its "idle" state with Espresso
}
}
Content copied to clipboard
Contents
#types
#idlingcoroutinescopes
#minimum-gradle-config
Types
IdlingDispatcherProviderRule | JUnit 4 Rule which automatically registers an IdlingDispatcherProvider with the IdlingRegistry |
IdlingDispatcher | A CoroutineDispatcher which tracks each dispatched coroutine using a CountingIdlingResource. All actual dispatches are delegated to a provided CoroutineDispatcher. |
IdlingDispatcherProvider | A special DispatcherProvider which guarantees that each of its properties is an IdlingDispatcher |
IdlingCoroutineScope | A special CoroutineScope which guarantees a property of an IdlingDispatcherProvider |
IdlingCoroutineScopes
Minimum Gradle Config
Add to your module's build.gradle.kts
:
repositories {
mavenCentral()
}
dependencies {
// core
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0")
implementation(platform("com.rickbusarow.dispatch:dispatch-bom:1.0.0-beta10"))
implementation("com.rickbusarow.dispatch:dispatch-core")
androidTestImplementation("com.rickbusarow.dispatch:dispatch-android-espresso")
// android
androidTestImplementation("androidx.test:runner:1.3.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}
Content copied to clipboard