在Kotlin中调用了Andoird Junit测试功能主线程块

时间:2019-12-11 02:27:20

标签: kotlin kotlin-coroutines

我想对viewModel中的一个函数进行单元测试,该函数包含带有获取主线程以更新liveData的暂停块。但是主线程部分中的代码块被调用。

主要思想是,在后端线程中从服务器获取数据,然后返回主线程以更新UI。

fun reloadItemss() {
        repositoryInterface.getItems {
            withContext(Dispatchers.Main) {
                // this part wasn't called.
                myLiveData.value = it
            }
        }
    }

测试代码

@Test
    fun reloadItemsTest() {

        viewModel.reloadItems()
        assertTrue(viewModel.myLiveData.value!!.isNotEmpty())

    }

repositoryInterface方法:

override fun getItems(completion: suspend (List<ItemEntity>) -> Unit) {
    GlobalScope.launch {
        completion(list)
    }
}

现在,我无法通过测试,因为未致电myLiveData.value = it。有什么帮助吗?谢谢!

0 个答案:

没有答案