Hi guys, I am new in Flow with unit testing. I wan...
# mockk
k
Hi guys, I am new in Flow with unit testing. I want to test this function
Copy code
internal suspend fun subscribeAndGetConnectionStateChange(connectGatt: () -> Unit): ConnectionStateChanged {
    val triggerEvents = watchGattCallback.triggerBluetoothGattCallbackEvents
    return triggerEvents.onSubscription {
        connectGatt()
        gattConnection = true
    }.firstOrNull {
        isGattSuccessAndStateConnected(it)
    } as ConnectionStateChanged? ?: getGattFailureAndStateDisconnected()
}
this
triggerBluetoothGattCallbackEvents
is a type of
MutableSharedFlow<GattTriggerEvent>(extraBufferCapacity = 50)
But I am getting error.
Copy code
@Test
    fun subscribeAndGetConnectionStateChange() {
        runBlocking {
            val mockWatchGattCallback = mockk<WatchGattCallback>()
            every { subject.watchGattCallback } returns mockWatchGattCallback

            // mock triggerBluetoothGattCallbackEvents
            val mockMutableSharedFlow = spyk(MutableSharedFlow<GattTriggerEvent>(extraBufferCapacity = 50))
            every { mockWatchGattCallback.triggerBluetoothGattCallbackEvents } returns mockMutableSharedFlow
            val mockFlow = slot<suspend FlowCollector<GattTriggerEvent>.() -> Unit>()
            val sharedFlow = MutableSharedFlow<GattTriggerEvent>()
            val flow = flow<GattTriggerEvent> {
                emit(mockk())
                emit(mockk())
            }

            sharedFlow.emitAll(flow)
            every { mockMutableSharedFlow.onSubscription(capture(mockFlow)) } returns sharedFlow
//            coAnswers {
//                mockFlow.captured.invoke(flow)
//                flow.emit(mockk())
//                mockk()
//            }

            val mockUnit = mockk<() -> Unit>()

            subject.subscribeAndGetConnectionStateChange(mockUnit)

            coVerify {

            }
        }
    }
Using junit4
Error
Copy code
Failed matching mocking signature for

left matchers: [slotCapture<Function2>()]
io.mockk.MockKException: Failed matching mocking signature for

left matchers: [slotCapture<Function2>()]