https://kotlinlang.org logo
#flow
Title
# flow
k

KotlinLeaner

03/03/2023, 1:13 PM
Hi guys, I have
MutableSharedFlow
like this
Copy code
private val triggerValue = MutableSharedFlow<BloodPressureMeasurement>()
val triggerBpValue: SharedFlow<BloodPressureMeasurement> = triggerValue
I am using in different viewmodel
Copy code
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
Copy code
@Test
fun `handleBpValue - `() = runTest {
    coEvery {
        someClass.triggerBpValue
    } returns MutableSharedFlow {
        emit(mockBloodPressureMeasurement)
    }


    subject.handleBpValue()

  
    verify {

    }
}
I am getting error in here
g

Giorgi

03/06/2023, 6:59 PM
You are directly invoking MutableSharedFlow. It has this signature:
Copy code
public 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
Copy code
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")
    }
k

KotlinLeaner

03/06/2023, 6:59 PM
Ohh nice thanks
Do you have any flow test examples ?
g

Giorgi

03/06/2023, 7:01 PM
nope, it is not an example, but I would recommend this video

https://www.youtube.com/watch?v=hzTU0lh-TIw

k

KotlinLeaner

03/06/2023, 7:02 PM
Thank you so much
I'll take a look
g

Giorgi

03/06/2023, 7:02 PM
🚀
2 Views