如何测试启动 viewModelScope 协程的 ViewModel 函数? Android 科特林
How test a ViewModel function that launch a viewModelScope coroutine? Android Kotlin
我正在尝试找出在函数成员上测试此类的最简单方法,我见过更复杂的情况,例如 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
我建议您完成整个 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"
}
然后在您的测试目录中创建这样的规则,注意 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)
}
}
我正在尝试找出在函数成员上测试此类的最简单方法,我见过更复杂的情况,例如 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
我建议您完成整个 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"
}
然后在您的测试目录中创建这样的规则,注意 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)
}
}