https://kotlinlang.org logo
#mockk
Title
# mockk
c

Carlos Tasada

11/21/2023, 10:52 AM
Hello there, I have a code that looks like that (simplified for this question)
Copy code
val publisher = mockk<CommandPublisher>(relaxed = true)
val something = Something(publisher)

val request = Request(field = "a")

something.doSomething(request)

verify {
   publisher.publish(any())
}
This works fine. But I want to confirm that the published object is the one I expect
Copy code
verify {
   publisher.publish(Event(request))
}
Where
Event(request)
maps some values to the published object. This check fails, because "arguments are not matching" when looking, the issue seems that
verify
is trying to compare by instance, but the expected instance is different. I want to compare by content. I saw that I could use
cmpEq
but I cannot find how to use it. Can someone provide an example of
verify
and
cmpEq
? Thanks a lot for your help
m

Mattia Tommasone

11/21/2023, 10:56 AM
IIUC, you should be able to do something like
Copy code
verify {
    publisher.publish(match {
        it.request.field == "a"
    })
}
Does this make sense to you?
c

Carlos Tasada

11/21/2023, 11:36 AM
Thanks a lot Mattia, this approach worked perfectly 😄
2 Views