android - 使用 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)
解决方案
造成这种情况的原因似乎是在库https://github.com/mockk/mockk/issues/554中发现的一个错误。continuation {}
正在对带有Kotlin 编译器添加的额外参数的函数调用进行验证。该错误尚未解决
推荐阅读
- dbus - 将 Secrets API 与 dbus-send 一起使用
- azure-active-directory - 撤销 Azure AD B2c 用户的登录会话不适用于本机应用程序
- laravel - Laravel 路由获取错误的控制器
- flutter - Flutter 在按下按钮或 onpressed 事件后授予相机和麦克风权限
- c++ - 在 C++ 中使用 sdl2 录制环回音频
- java - 如何在不访问源代码的情况下解决杰克逊中属性的 getter 定义冲突
- python - 如何根据给定的基准日期将日期分成特定的时间范围
- mongodb - E11000 重复键错误收集 {info.subs: null}
- c++ - 在cpp中获取图像的公制尺寸
- elasticsearch - 对两个字段进行聚合会为其中一个返回空值