模拟验证lambda参数 [英] mockk verify lambda argument
本文介绍了模拟验证lambda参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证通过lamdba传递的值. 该功能看起来像这样:
I'd like to verify the value which was passed in via a lamdba. The func looks like this:
fun save(entity: Any, idSupplier: () -> UUID): JsonEntity {
return save(JsonEntity(idSupplier(), entity, entity::class.simpleName!!))
}
现在在我的测试中,我想验证为idSupplier传递的值.我做了一个模拟以返回save(...)的值,这样在我自己的save(...,()-> ...)中调用
Now within my test I'd like to verify the value which has been passed in for the idSupplier. I made a mock to return a value for the save(...) which is called in my own save(..., () -> ...) like this
every { jsonStorage.save(any<JsonEntity>()) } answers { value }
现在验证我现在有这个
verify(exactly = 1) { jsonStorage.save(event, any()) }
正在工作,但是我想知道已传递的确切值,即,如果实体的id为123,我想验证一下.
Which is working, but I'd like to know the exact value which has been passed, i.e. if the entity's id was 123, I'd like to verify this.
提前谢谢
推荐答案
您需要一个插槽用于捕获参数一个>.
You need a Slot for capturing the parameters.
示例
val id = slot<UUID>()
every { save(any<JsonEntity>()) { capture(id)} } answers { value }
// `id.captured` contains the value passed
// as a parameter in the lambda expression `idSupplier`
assertEquals(UUID.fromString("4195f789-2730-4f99-8b10-e5b9562210c1"), id.captured)
这篇关于模拟验证lambda参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文