How to capture arguments with io.mockk in unit-test

882 views Asked by At

enter image description hereI am having difficulties when trying to use a CapturingSlot<*> from mockk.

I get the error message shown in the above image

See the paragraph named Capturing in https://mockk.io

As explained in the user guide, I have tried val car = mockk<Car>() but also val car: CapturingSlot<Car> = slot() but both attempts results in the error message listet above...

Anyone with suggestions?

This is a compile-time error and I am using the following dependency in my Gradle build file: testImplementation("io.mockk:mockk-jvm:1.13.8")

1

There are 1 answers

0
AndrewL On

Jactor-rises, maybe some example code will help:

In this specimen code, imagine that consumer is the code under test. It takes 3 args and during it operation it will make call to someApi.someCall(...) with 3 arguments and you want to check the middle one contains the expected data:

import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify

val analysisSlot = slot<Analysis>()
every { someApi.someCall(SET_ID, capture(analysisSlot), any()) } returns mockk()

consumer.accept(questionSet, authHeaders(), mockk())

analysisSlot.captured shouldBe Analysis(
    ...
)
verify {
    channelsomeApi.someCall(any(), any(), any())
}