我正在尝试找到一种方法来很好地实现一个轮询CoroutineDispatcher的isActive
属性的IdlingResource。但是,从调试情况来看,在检查此属性时,似乎从来没有活动的Job。
到目前为止,我已经尝试使用AsyncTask的THREAD_POOL_EXECUTOR
进行内置空闲,但是当使用asCoroutineDispatcher
扩展函数并使用生成的CoroutineDispatcher
启动视图模型的作业时,它似乎不起作用。我曾尝试编写自定义的IdlingResource
ViewModel
fun authenticate(username: String, password: String) = viewModelScope.launch(Dispatchers.Default) {
if (_authenticateRequest.value == true) {
return@launch
}
_authenticateRequest.postValue(true)
val res = loginRepo.authenticate(username, password)
_authenticateRequest.postValue(false)
when {
res is Result.Success -> {
_authenticateSuccess.postValue(res.item)
}
res is Result.Failure && res.statusCode.isHttpClientError -> {
_authenticateFailure.postValue(R.string.invalid_password)
}
else -> {
_authenticateFailure.postValue(R.string.network_error)
}
}
}
IdlingResource
class CoroutineDispatcherIdlingResource(
private val resourceName: String,
private val dispatcher: CoroutineDispatcher
) : IdlingResource {
private var callback: IdlingResource.ResourceCallback? = null
override fun getName() = resourceName
override fun isIdleNow(): Boolean {
if (dispatcher.isActive) { return false }
callback?.onTransitionToIdle()
return true
}
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
this.callback = callback
}
}
Espresso测试
@RunWith(AndroidJUnit4::class)
class LoginIntegrationTest {
@get:Rule
val activityRule = ActivityTestRule(MainActivity::class.java)
var idlingResource: CoroutineDispatcherIdlingResource? = null
@Before
fun before() {
idlingResource = CoroutineDispatcherIdlingResource(this.javaClass.simpleName, Dispatchers.Default)
IdlingRegistry.getInstance().register(idlingResource)
}
@Test
fun loginFailure() {
onView(withId(R.id.username))
.perform(clearText()).perform(typeText("aslkdjqwe"))
onView(withId(R.id.password))
.perform(clearText()).perform(typeText("oxicjqwel"))
onView(withId(R.id.login_button))
.perform(click())
onView(withId(com.google.android.material.R.id.snackbar_text))
.check(matches(withText(R.string.invalid_password)))
}
}
我期望一旦调用身份验证‘ViewModel’函数,isActive
属性就会为true,但事实似乎并非如此。它总是看起来是假的,因为CoroutineDispatcher中从来没有活动的作业。
https://stackoverflow.com/questions/56583557
复制相似问题