Mockk 库 CoroutineScope ClassCastException

Mockk library CoroutineScope ClassCastException

我正在尝试使用 Mockk 库来模拟下面的这个函数。

   fun launchOn(block: suspend CoroutineScope.() -> Unit) {
        viewModelScope.launch(Dispatchers.IO) { block() }
    }

我试着在下面模拟一下

private val viewModel: PersonalInfoViewModel = mockk(relaxed = true)
every { viewModel.launchOn(any()) } answers {callOriginal()}

但我得到 ClassCastException。我也尝试使用 coEveryhint 但没有任何改变。我在文档中找不到关于这种情况的太多信息。

我的问题是,我哪里弄错了?模拟此功能的正确方法是什么?

谢谢

异常:


class java.lang.Object cannot be cast to class kotlinx.coroutines.CoroutineScope (java.lang.Object is in module java.base of loader 'bootstrap'; kotlinx.coroutines.CoroutineScope is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @17490d9e)
java.lang.ClassCastException: class java.lang.Object cannot be cast to class kotlinx.coroutines.CoroutineScope (java.lang.Object is in module java.base of loader 'bootstrap'; kotlinx.coroutines.CoroutineScope is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @17490d9e)
    at androidx.lifecycle.ViewModelKt.getViewModelScope(ViewModel.kt:37)
    at com.biletdukkani.common.base.BaseViewModel.launchOn(BaseViewModel.kt:106)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
    at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:83)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:20)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$invocation$$inlined$stdFunctions$lambda.invoke(JvmMockFactoryHelper.kt:28)
    at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString.invoke(MockKStub.kt:227)
    at io.mockk.MockKAnswerScope.callOriginal(API.kt:2184)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions.invoke(PersonalInfoViewModelTest.kt:57)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions.invoke(PersonalInfoViewModelTest.kt:57)
    at io.mockk.MockKStubScope$answers.invoke(API.kt:2092)
    at io.mockk.MockKStubScope$answers.invoke(API.kt:2069)
    at io.mockk.FunctionAnswer.answer(Answers.kt:19)
    at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:14)
    at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:54)
    at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
    at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
    at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler.invocation(JvmMockFactoryHelper.kt:25)
    at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
    at com.biletdukkani.common.base.BaseViewModel.launchOn(BaseViewModel.kt:107)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModel.getProfileInfo(PersonalInfoViewModel.kt:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
    at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:83)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:20)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$invocation$$inlined$stdFunctions$lambda.invoke(JvmMockFactoryHelper.kt:28)
    at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString.invoke(MockKStub.kt:227)
    at io.mockk.MockKAnswerScope.callOriginal(API.kt:2184)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions.invoke(PersonalInfoViewModelTest.kt:59)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions.invoke(PersonalInfoViewModelTest.kt:59)
    at io.mockk.MockKStubScope$answers.invoke(API.kt:2092)
    at io.mockk.MockKStubScope$answers.invoke(API.kt:2069)
    at io.mockk.FunctionAnswer.answer(Answers.kt:19)
    at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:14)
    at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:54)
    at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
    at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
    at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler.invocation(JvmMockFactoryHelper.kt:25)
    at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModel.getProfileInfo(PersonalInfoViewModel.kt:32)
    at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest.getProfileInfo(PersonalInfoViewModelTest.kt:72)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:61)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:61)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner.evaluate(RobolectricTestRunner.java:575)
    at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=12=](SandboxTestRunner.java:278)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=12=](Sandbox.java:89)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

我想通了。

我将我的功能分为两部分。然后,我嘲笑了 executeProfileInfoGet 部分。现在,我不需要再模拟 launchOn 函数了。

函数:

    fun getProfileInfo() = launchOn {
        executeProfileInfoGet()
    }

    suspend fun executeProfileInfoGet(){
        executeFlow(profileInfoRepository.getProfileInfo()) {
            when (it) {
                is DataState.Success -> {
                    initCaches(it.response)
                    checkPassportFields(it.response)
                    profileInfoLiveData.postValue(it.response)
                }
                is DataState.Error -> {
                    showError(it.error.message)
                }
                else -> Unit
            }
        }
    }

在测试中模拟 class:

coEvery { viewModel.executeProfileInfoGet() } coAnswers { callOriginal() }