gpt4 book ai didi

android - 如何测试启动 viewModelScope 协程的 ViewModel 函数?安卓 Kotlin

转载 作者:行者123 更新时间:2023-12-05 00:14:29 26 4
gpt4 key购买 nike

我试图找出在函数成员上测试这种类型的最简单方法,我见过更复杂的情况,例如 Coroutines - unit testing viewModelScope.launch methods但没有解决

ListScreenViewModel.kt

@HiltViewModel
class ListScreenViewModel @Inject constructor(): ViewModel() {

private var _itemsNumber = mutableStateOf(0)

private var _testList = mutableStateOf(listOf<String>())
val testList = _testList

fun addItem() {
viewModelScope.launch {
_itemsNumber.value++
_testList.value += (
"Item ${_itemsNumber.value}"
)
}
}
}

ListScreenViewModelTest.kt

class ListScreenViewModelTest{

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)
}
}

Error message

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

最佳答案

你需要使用一个叫做 TestCoroutineDispatcher 的东西。在本地单元测试期间以及使用它的最佳方式创建 Rule .
您可以在此处详细阅读:https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-survey#3
我建议您通过整个代码实验室。这将非常有帮助。
更新版本 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"
}
然后在您的测试目录中创建这样的规则,注意 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()
}
}
像这样使用,注意 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)
}
}
原答案:
解决方案
添加此依赖项:
 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"
}
然后在您的测试目录中创建这样的规则:
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()
}
}
像这样使用它:
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)
}
}

关于android - 如何测试启动 viewModelScope 协程的 ViewModel 函数?安卓 Kotlin ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71807957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com