KotlinLeaner
02/21/2023, 3:28 PMinternal 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.@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 junit4Failed matching mocking signature for
left matchers: [slotCapture<Function2>()]
io.mockk.MockKException: Failed matching mocking signature for
left matchers: [slotCapture<Function2>()]