Can I use `capture(...)` as a parameter for exten...
# mockk
m
Can I use
capture(...)
as a parameter for extension functions? e.g.
Copy code
val slot = slot<String>()

capture(slot).snakeCase()
I got this:
Copy code
kotlin.UninitializedPropertyAccessException: lateinit property captured has not been initialized
	at io.mockk.CapturingSlot.getCaptured(API.kt:2503)
As I see, the receiver wasn't captured. Any ideas?
k
I don't understand what you're trying to do. Shouldn't it be something like this?
Copy code
val slot = slot<String>()
every { capture(slot).snakeCase() } returns "a_b_c"
val res = "xyz".snakeCase()
println(slot.captured)
Have you initialised with
mockkStatic
first?
m
I want the example function return the passed string (no-op, basically) Yeah, I've used
mockkStatic
Copy code
val slot = slot<String>()
every {
    capture(slot).snakeCase()
} returns slot.captured // <- fails here
wait, maybe I should do this:
Copy code
val slot = slot<String>()
every {
    capture(slot).snakeCase()
} answers {
    slot.captured
}
let me try
k
The above should work. Also try:
Copy code
every { any().snakeCase() } answers { firstArg() }
👍 1
m
Your suggestion works perfectly! Thanks. Also, note that I had to use
any<String>()
instead.
👍 1
769 Views