how to see why mockk assertEquals(...) fails

83 views Asked by At

so i have a test like this:

verify {
            function.send(
                withArg { assertEquals(expected, actual) },
                withArg { assertEquals(expected, actual) })
}

and i'm wondering how can i easily see in stacktrace why the result was - (the diff between expected and actual object)

[1]: argument: ...responseCode=0003, usdAmount=12), matcher: matcher\<Transaction\>(), result: -
[2]: argument: CHARGEBACK, matcher: matcher\<Reason\>(), result: +

Stack trace:
io.mockk.impl.InternalPlatform.captureStackTrace (InternalPlatform.kt:125)
io.mockk.impl.stub.MockKStub.handleInvocation (MockKStub.kt:250)
...

i already added settings as doc says:

stackTracesOnVerify=true
stackTracesAlignment=left
1

There are 1 answers

0
fxshlein On

Depending on your exact case, you might be able to use capturing slots while verifying the calls. This would allow you to do the assertions outside of the verify block:

val firstArgument = slot<TypeOfFirst>()
val secondArgument = slot<TypeOfSecond>()

verify {
    function.send(capture(firstArgument), capture(secondArgument))
}

assertEquals(expected, firstArgument.captured)
assertEquals(expected, secondArgument.captured)

Note that, inside the verify block, this will now be equivalent to matching function.send(any(), any()), which could change the result when your actual verify block is more complicated (for example, matching multiple calls of the send function)