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

Mikhail

07/15/2022, 7:37 PM
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

Klitos Kyriacou

07/17/2022, 4:15 PM
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

Mikhail

07/17/2022, 4:18 PM
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

Klitos Kyriacou

07/17/2022, 4:38 PM
The above should work. Also try:
Copy code
every { any().snakeCase() } answers { firstArg() }
👍 1
m

Mikhail

07/17/2022, 4:45 PM
Your suggestion works perfectly! Thanks. Also, note that I had to use
any<String>()
instead.
👍 1
708 Views