KotlinLeaner
03/03/2023, 1:13 PMMutableSharedFlow
like this
private val triggerValue = MutableSharedFlow<BloodPressureMeasurement>()
val triggerBpValue: SharedFlow<BloodPressureMeasurement> = triggerValue
I am using in different viewmodel
internal fun handleBpValue() {
viewModelScope.launch(context = <http://Dispatcher.IO|Dispatcher.IO>) {
someClass.triggerBpValue.collectLatest { measurement ->
// code in here
}
}
}
I am trying to unit test in mockk
KotlinLeaner
03/03/2023, 1:14 PM@Test
fun `handleBpValue - `() = runTest {
coEvery {
someClass.triggerBpValue
} returns MutableSharedFlow {
emit(mockBloodPressureMeasurement)
}
subject.handleBpValue()
verify {
}
}
I am getting error in hereGiorgi
03/06/2023, 6:59 PMpublic fun <T> MutableSharedFlow(
replay: Int = 0,
extraBufferCapacity: Int = 0,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): MutableSharedFlow<T> {
when you use curly braces you are passing a last argument, which requires type of onBufferOverflow: BufferOverflow
, so it complains about that type.
so try something like
val triggerValue = MutableSharedFlow<String>()
@Test
fun `some test name here`() = runTest {
every { // I think it does not need coEvery, but not sure
triggerValue as Flow<String>
} returns flowOf("123")
}
KotlinLeaner
03/06/2023, 6:59 PMKotlinLeaner
03/06/2023, 7:00 PMKotlinLeaner
03/06/2023, 7:02 PMKotlinLeaner
03/06/2023, 7:02 PMGiorgi
03/06/2023, 7:02 PM