看起来,Mockk 模拟只存储对接收到的参数的引用以供以后验证。一旦参数在整个测试过程中被修改,这就会出现问题。如何验证模拟函数调用的参数深度相等?
以下代码片段演示了该问题:
data class Entity(var status: String)
interface Processor {
fun process(entity: Entity)
}
class ProcessorTest {
@Test
fun `should receive untouched entity`() {
val myEntity = Entity("untouched")
val processorMock: Processor = mockk()
every { processorMock.process(any()) }.answers {
println(firstArg<Entity>().status)
}
processorMock.process(myEntity) // process untouched entity
myEntity.status = "touched" // afterwards it becomes touched
verify { processorMock.process(Entity("untouched")) }
}
}
Run Code Online (Sandbox Code Playgroud)
processorMock::process预期并且显然是用“未触及”实体调用的,因为它打印“未触及”。但是验证失败并显示:
java.lang.AssertionError: Verification failed: call 1 of 1: Processor(#1).process(eq(Entity(status=untouched)))). Only one matching call to Processor(#1)/process(Entity) happened, but arguments are not matching:
[0]: argument: Entity(status=touched), matcher: eq(Entity(status=untouched)), result: -
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 a CapturingSlot,但没有成功。
Mockk 版本是 1.9.3,Kotlin 版本是 1.3.70。我使用 JUnit 5 作为测试框架。
任何帮助都感激不尽!
| 归档时间: |
|
| 查看次数: |
1361 次 |
| 最近记录: |