Hello how could I test a function that receives a ...
# coroutines
p
Hello how could I test a function that receives a
Flow<T>
as a parameter and then inside has a
collect { }
? Example
Copy code
fun doStuff(flow: Flow<String>) {
   scope.launch {
      flow.collect { 
        if(!it in myList) { 
           dataSource.add(it)
        }
      }
   }
}
How would I have this verify that
dataSource.add(it)
? Considering this
myList
is something I can mock too
j
Checking data source before and after to see if it is adding what you expect
p
Yes but how, I mean do I have to do something like :
Copy code
runBlocking {
   myMethod.doStuff(flowOf("hello"))
}
How would be the pseudo to test it?
j
You shouldn't need runBlocking, only the new runTest function. It allows advance the time too until the flow is completed
p
but do I have to emit the value?
j
you mean the flowOf?
p
yes
j
Probably you should test multiple scenarios
with empty list, with a unique list, another one to check if the if condition is working and so on