Cannot Capture Mutable List Mockk

Kotlin error when compiling such code mentioned in MockK | mocking library for Kotlin. What’s wrong actually?

class Foo{
   fun fx(parm: MutableList<Double>) {}
}

val foo = Foo()
val parm: MutableList<Double> = mutableListOf()

every { foo.fx(capture(parm)) }

Error written below

Kotlin: Type mismatch: inferred type is Double but MutableList<Double> was expected

I believe what you really wanted to do was this:

class Foo{
	fun fx(parm: MutableList<Double>) {}
}

val foo = Foo()
val parm = slot<MutableList<Double>>()

every { foo.fx(capture(parm)) }

The capture() function you used basically was meant to capture single values into your list, but it wasn’t capturing a list you would try to pass as a function argument. To make it work you would need to write this instead:

class Foo{
	fun fx(parm: MutableList<Double>) {}
}

val foo = Foo()
val parm = mutableListOf<MutableList<Double>>()

every { foo.fx(capture(parm)) }
1 Like

Thanks a lot for the answer, it works. I was misunderstood the usage of Slot and MutableList. I thought that MutableList used when the argument is in List type. It’s used when multiple invocation’s argument were expected to be recorded instead