Hello guys, I was trying out flow and did implemen...
# coroutines
k
Hello guys, I was trying out flow and did implement following example:
Copy code
flowOf("one string", "second string", "third string")
            .debounce(300)
            .map { it.trim() }
            .filter { it.length > 2 }
            .flowOn(Dispatchers.Default)
            .distinctUntilChanged()
            .switchMap {
                // Simulate server call
                delay(2000)
                flow {
                    // Just return search term for the moment
                    emit(it)
                }
            }
            .flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
            .collect {
                _viewState.value = it
            }
Now I want to define a unit test for that but I am not sure how to do this for the above flow. Do I need to extract the flow except from the
collector
at the end to a function and then add a test
collector
and verify the values I would expect to get there? Or is there any other idiomatic way?
s
You’d want to test the `_viewState.value`’, correct?
Mock or implement the
_viewState
and verify if its
value
is called with the correct values (in the correct order). Since you hard-coded the Dispatchers, wrap the entire call into a
runBlocking { ... }
and when that one returns, assert/verify the
_viewState.value
.