kap*_*inz 5 android ui-testing dispatcher coroutine kotlin
我想在 Android 中测试以下非常常见的用例作为仪器测试:
这是我的 Viewmodel 中的函数:
fun fetch() {
_loading.value = true //loading is shown
viewModelScope.launch {
val results = fetchUseCase() //suspend function
_result.postValue(results)
_loading.postValue(false) //loading is not displayed
}
}
Run Code Online (Sandbox Code Playgroud)
@HiltAndroidTest
@UninstallModules(CoroutinesDispatcherModule::class)
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTestJunit4Deprecated {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
@Before
fun setup() {
ActivityScenario.launch(HomeScreenActivity::class.java)
}
@ExperimentalCoroutinesApi
@Test
fun fetchTest() {
//pausing the long running tasks
mainCoroutineRule.pauseDispatcher()
//When clicking the button
onView(withId(R.id.load_measurement_button)).perform(click())
//loading is shown
onView(withId(R.id.loading_overlay))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
//continue fetch
mainCoroutineRule.resumeDispatcher()
// loading is not shown anymore and the result is there
onView(withId(R.id.loading_overlay))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)))
onView(withId(R.id.message))
.check(matches(withText("0")))
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,“pauseDispatcher()”和“resumeDispatcher”已被弃用。我尝试使用“StandardTestDispatcher”和“advanceUntilIdle()”,但它无法按预期工作。协程永远不会恢复。如何重写该测试,使其有效: