i’m having trouble with verification of suspending...
# test
m
i’m having trouble with verification of suspending functions with mockito failing randomly. the tests most often pass, but will occasionally fail and i’m not sure why. for example:
Copy code
@Test
    fun `Submits app selection if selections can be made`() {
        mainActivityViewModel.submitAppSelection(selectedApp)

        runBlocking {
            verify(mockAppsStartupFsm).submitEvent(AppSelected(selectedApp))
        }
    }
Copy code
class MainActivityViewModel {
    fun submitAppSelection(app: App) {
        if (!selectionsCanBeMade()) return
        lastSelectedApp = app

        val coroutineScope = CoroutineScope(Dispatchers.Default)
        coroutineScope.launch { appsStartupFsm.submitEvent(AppSelected(app)) }
    }
}
AppsStartupFsm#submitEvent
is a suspending function. Changing the runBlocking scope to be function level doesn’t seem to help. Is there something I’m missing with verification of suspending functions?
It didn't work for me though
If you got it to work I'd be really happy to hear about it 🙂
m
The issue is that
launch
may not actually launch the coroutine before
verify
runs. Verification of suspending functions works, you just have to do it right. I’m still working on how to fix it in my own code. In the simplified example I posted it would need to be something like
Copy code
suspend fun submitAppSelection(app: App) {
        if (!selectionsCanBeMade()) return
        lastSelectedApp = app

        appsStartupFsm.submitEvent(AppSelected(app)
    }
and then use runblocking scope for entire test function
s
I don't think moving scope to activity/fragment is a good idea
Are you using coroutines android and Dispatchers.Main?