我使用这个帖子的扩展函数:https://stackoverflow.com/a/72111931/1305993
在我的测试中有:
@Before
fun setUp() {
savedStateHandle = SavedStateHandle()
savedStateHandle["areaId"] = 1L
saveImageUseCase = Mockito.mock(SaveImageUseCase::class.java)
repo = FakeRepository()
viewModel = AreaEditViewModel(repo, savedStateHandle, saveImageUseCase)
}
@Test
fun setUpArea() = runTest {
viewModel.callPrivateFunc("setUpArea", 2L)
}
在我的viewModel我有:
private suspend fun setUpArea(areaId: Long) {
repository.getAreaById(areaId).first() {
savedStateHandle["area"] = it
true
}
}
当我运行测试时,我得到:Callable期望3个参数,但是提供了2个。java.lang.IllegalArgumentException: Callable期望3个参数,但是提供了2个。
我不知道第三个参数是什么
我需要callSuspended而不是call
suspend inline fun <reified T> T.callSuspendedPrivateFunc(name: String, vararg args: Any?): Any? =
T::class
.declaredMemberFunctions
.firstOrNull { it.name == name }
?.apply { isAccessible = true }
?.callSuspend(this, *args)