Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mockk verify lambda argument

Tags:

kotlin

mockk

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

like image 256
tomhier Avatar asked Dec 24 '18 09:12

tomhier


1 Answers

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)
like image 130
Omar Mainegra Avatar answered Oct 31 '22 22:10

Omar Mainegra