首页 > 解决方案 > 使用 MockK coVerify 时,未调用方法

问题描述

以下测试失败。我已经手动测试了它,它可以工作。但我不确定为什么不调用该方法。我想这与我在协程中测试一个方法的事实有关,但是,我正在使用TestRule将调度程序设置为testDispatcher注入到演示者的 a 。

我希望你能帮忙。欢迎对代码的任何部分提出任何意见。谢谢!


测试班

@RunWith(JUnit4::class)
class RegisterPresenterTest {

    @get:Rule
    var coroutinesTestRule = CoroutineTestRule()

    @MockK
    lateinit var viewMock: IRegisterView

    @MockK
    lateinit var service : UserManagementServiceImpl

    private lateinit var presenter: RegisterPresenter

    @Before
    fun setUp() {
        MockKAnnotations.init(this)
        presenter =
            RegisterPresenter(viewMock, coroutinesTestRule.testDispatcherProvider)
    }

    @Test
    fun `shouldCreateNewUser`() = coroutinesTestRule.testDispatcher.runBlockingTest {
        // given
        val userDetails = UserDetails(
            username = "user1", password = "123",
            name = "Juan", surname = "Doe", email = "user1@bbc.com"
        )
        coEvery {
                service.verifyUserIsNotInDatabase(userDetails.username, userDetails.email)
        } returns UserExistence(usernameInDatabase = false, emailInDatabase = false)

        // when
        presenter.createNewUser(userDetails)

        // then
        coVerify(exactly = 1) { service.createNewUser(userDetails) }
    }
}

主持人

class RegisterPresenter(val view: IRegisterView,
                        private val dispatchers: DispatcherProvider = DefaultDispatcherProvider()){

    private val userManagementServiceImpl = UserManagementServiceImpl()

    fun createNewUser(userDetails: UserDetails) {

        // verify the user does not exist in database
        var userExistence: UserExistence? = null
        GlobalScope.launch(dispatchers.default()) {
            try {
                userExistence = userManagementServiceImpl
                    .verifyUserIsNotInDatabase(userDetails.username, userDetails.email)

                withContext(dispatchers.main()) {
                    if (userExistence != null) {
                        if (userExistence!!.emailInDatabase) {
                            view.displayEmailInDatabaseError()
                        }

                        if (userExistence!!.usernameInDatabase) {
                            view.displayUsernameInDatabaseError()
                        }

                        if (!userExistence!!.usernameInDatabase && !userExistence!!.emailInDatabase) {
                            GlobalScope.launch(dispatchers.default()) {
                                try {
                                    val userCreated = userManagementServiceImpl.createNewUser(userDetails)

                                    withContext(dispatchers.main()) {
                                        when (userCreated) {
                                            true -> 1 // TODO Go to user successfully created screen
                                            false -> view.displayAccountErrorDialog()
                                        }
                                    }
                                } catch (e: ConnectException) {
                                    view.displayUnableToConntectDialog()
                                }
                            }
                        }
                    }
                }
            } catch (e: ConnectException) {
                view.displayUnableToConntectDialog()
            }
        }
    }
}

连接到改造服务的存储库

class UserManagementServiceImpl(
    private val service: IUserManagementService = ServiceAPIFactory.createService()){

      suspend fun createNewUser(userDetails: UserDetails) : Boolean = service.createUser(userDetails)
      suspend fun verifyUserIsNotInDatabase(username : String, email : String)
             = service.verifyUserIsNotInDatabase(username, email)
}

堆栈跟踪

java.lang.AssertionError: Verification failed: call 1 of 1: UserManagementServiceImpl(service#2).createNewUser(eq(UserDetails(username=user1, password=123, email=user1@bbc.com, name=Juan, surname=Doe)), any())) was not called

at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalCoVerify(API.kt:143)
at io.mockk.MockKKt.coVerify(MockK.kt:169)
at io.mockk.MockKKt.coVerify$default(MockK.kt:166)
at com.example.capitalcityquizktx.RegisterPresenterTest$shouldCreateNewUser$1.invokeSuspend(RegisterPresenterTest.kt:78)
at com.example.capitalcityquizktx.RegisterPresenterTest$shouldCreateNewUser$1.invoke(RegisterPresenterTest.kt)
at kotlinx.coroutines.test.TestBuildersKt$runBlockingTest$deferred$1.invokeSuspend(TestBuilders.kt:50)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.test.TestCoroutineDispatcher.dispatch(TestCoroutineDispatcher.kt:50)
at kotlinx.coroutines.DispatchedKt.resumeCancellable(Dispatched.kt:423)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:154)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt:89)
at kotlinx.coroutines.BuildersKt.async(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async$default(Builders.common.kt:82)
at kotlinx.coroutines.BuildersKt.async$default(Unknown Source)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:49)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:78)
at com.example.capitalcityquizktx.RegisterPresenterTest.shouldCreateNewUser(RegisterPresenterTest.kt:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.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$1.evaluate(TestWatcher.java:61)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

标签: androidkotlinmockk

解决方案


造成这种情况的原因似乎是在库https://github.com/mockk/mockk/issues/554中发现的一个错误。continuation {}正在对带有Kotlin 编译器添加的额外参数的函数调用进行验证。该错误尚未解决


推荐阅读