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?
If you got it to work I'd be really happy to hear about it 🙂
m
matt tighe
01/18/2019, 7:21 PM
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)
}
matt tighe
01/18/2019, 7:21 PM
and then use runblocking scope for entire test function
s
Saiedmomen
01/18/2019, 7:38 PM
I don't think moving scope to activity/fragment is a good idea
Saiedmomen
01/18/2019, 7:40 PM
Are you using coroutines android and Dispatchers.Main?