I want to verify the number of calls being invoked by the lambda.
This lambda serves as a callback to deliver the state changes. I want to constraint the lambda to be used only via constructor.
Is there any way to test this when lambda is in constructor param?
Tried the following but this doesn't seem to work.
class SampleTest {
private lateinit var sut: Sample
@Test
fun `test lambda is called`() {
val captureCallback = slot<(String) -> Unit>()
every {
sut = Sample(capture(captureCallback))
} answers {
captureCallback.captured("")
}
verify(exactly = 1) {
captureCallback.captured("")
}
}
}
class Sample(val onClick: (String) -> Unit) {
init {
triggerLambda()
}
private fun triggerLambda() {
onClick("")
}
}
In case someone is still looking here is how I solved it
class SampleTest {
private val callback: (String) -> Unit = mockk(relaxed = true)
@Test
fun `test lambda is called`() {
Sample(callback)
verify(exactly = 1) { callback.invoke("") }
}
}