How to test composables with ViewModels with flows...
# compose
j
How to test composables with ViewModels with flows inside the viewmodels that run in viewModelScope? If I use
Dispatchers.setMain(UnconfinedTestDispatcher)
the android tests time out and If i remove the line the JVM/Desktop tests fail and ask me to use
Dispatcherrs.setMain(UconfinedTestDispatcher)
Copy code
@BeforeTest
fun setUp() {
    // This fails android but commenting it fails jvm because of the coroutines in BalanceViewModel
    Dispatchers.setMain(UnconfinedTestDispatcher())
The flows in the viewModel looks like this:
Copy code
private fun observeSelectedAccount()
    {
        accountJob?.cancel()
        accountJob = viewModelScope.launch(dispatcher) {
            selectedAccountUi2.onEach {
                it?.let { account ->
                    setFiatBalance(account)
                    observeBalance(account)
                }
            }.launchIn(this)
        }
    }
a
View models have a constructor that takes the viewmodelscope
Then you should be able to pass the test scope
j
I still get the timeout after passing a testScope:
Copy code
private val testDispatcher = StandardTestDispatcher()
    private val testScope = TestScope(testDispatcher)
a
Timeout typically means that there are still active jobs that do not complete at end of the test scope