mockk verify lambda argument

∥☆過路亽.° 提交于 2019-12-10 14:23:16

问题


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!!))
}

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 }

Now on verify I have this now

verify(exactly = 1) { jsonStorage.save(event, any()) }

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.

Thank you in advance


回答1:


You need a Slot for capturing the parameters.

Example

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)


来源:https://stackoverflow.com/questions/53911556/mockk-verify-lambda-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!