如何测试启动 viewModelScope 协程的 ViewModel 函数?安卓科特林

Ole*_*pez 19 android unit-testing viewmodel kotlin kotlin-coroutines

我\xc2\xb4m试图找出在函数成员上测试这种类型的最简单方法,我\xc2\xb4见过更复杂的情况,例如协程 - 单元测试 viewModelScope.launch 方法,但没有\xc2\xb4t解决

\n
\n

ListScreenViewModel.kt

\n
\n
@HiltViewModel\nclass ListScreenViewModel @Inject constructor(): ViewModel() {\n\n    private var _itemsNumber = mutableStateOf(0)\n\n    private var _testList = mutableStateOf(listOf<String>())\n    val testList = _testList\n\n    fun addItem() {\n        viewModelScope.launch {\n            _itemsNumber.value++\n            _testList.value += (\n                "Item ${_itemsNumber.value}"\n                )\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

ListScreenViewModelTest.kt

\n
\n
class ListScreenViewModelTest{\n\n    private lateinit var viewModel: ListScreenViewModel\n\n    @Before\n    fun setup(){\n        viewModel = ListScreenViewModel()\n    }\n\n    @Test\n    fun `add an item to the list of items`(){\n        val numberOfItems = viewModel.testList.value.size\n        viewModel.addItem()\n        assert(viewModel.testList.value.size == numberOfItems+1)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

错误信息

\n
\n
Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used\n\n
Run Code Online (Sandbox Code Playgroud)\n

May*_*jra 28

您需要使用TestCoroutineDispatcher在本地单元测试期间调用的东西以及使用它创建Rule.

您可以在这里详细阅读:https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-survey#3

我建议您完成整个 Codelab。这真的很有帮助。

版本更新1.6.1

基于此迁移指南:https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md

testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1") {
        // https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
    } 
Run Code Online (Sandbox Code Playgroud)

然后在测试目录中创建如下规则,注意更改StandardTestDispatcher

@ExperimentalCoroutinesApi
class MainCoroutineRule(private val dispatcher: TestDispatcher = StandardTestDispatcher()) :
    TestWatcher() {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(dispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
    }
}
Run Code Online (Sandbox Code Playgroud)

runTest像这样使用它,注意&的用法advanceUntilIdle

@OptIn(ExperimentalCoroutinesApi::class)
class ListScreenViewModelTest {

    @ExperimentalCoroutinesApi
    @get:Rule
    var mainCoroutineRule = MainCoroutineRule()

    private lateinit var viewModel: ListScreenViewModel

    @Before
    fun setUp() {
        viewModel = ListScreenViewModel()
    }


    @Test
    fun `add an item to the list of items`() = runTest {
        val numberOfItems = viewModel.testList.value.size
        viewModel.addItem()
        advanceUntilIdle()
        assert(viewModel.testList.value.size == numberOfItems + 1)
    }
}
Run Code Online (Sandbox Code Playgroud)

原答案:

对于解决方案

添加此依赖项:

 testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2") {
        // https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android
        exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
    }
Run Code Online (Sandbox Code Playgroud)

然后在测试目录中创建如下规则:

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

@ExperimentalCoroutinesApi
class MainCoroutineRule(val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) :
    TestWatcher(),
    TestCoroutineScope by TestCoroutineScope(dispatcher) {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(dispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        cleanupTestCoroutines()
        Dispatchers.resetMain()
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class ListScreenViewModelTest {

    @ExperimentalCoroutinesApi
    @get:Rule
    var mainCoroutineRule = MainCoroutineRule()

    private lateinit var viewModel: ListScreenViewModel

    @Before
    fun setup(){
        viewModel = ListScreenViewModel()
    }

    @Test
    fun `add an item to the list of items`(){
        val numberOfItems = viewModel.testList.value.size
        viewModel.addItem()
        assert(viewModel.testList.value.size == numberOfItems+1)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在最新版本中,TestCoroutineScope 和 TestCoroutineDispatcher 已被弃用。您可以使用最新的 api 更新解决方案吗?谢谢 (2认同)