Skip to content

Instantly share code, notes, and snippets.

@Groostav
Created January 11, 2019 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Groostav/5870edde62a40235f6dc8e67c7830437 to your computer and use it in GitHub Desktop.
Save Groostav/5870edde62a40235f6dc8e67c7830437 to your computer and use it in GitHub Desktop.
class Tests{
@Test
fun `Submits app selection if selections can be made`() = runBlocking(Dispatchers.Main) { //@Test... = runBlocking is a good convention
mainActivityViewModel.submitAppSelection(selectedApp)
verify(mockAppsStartupFsm).submitEvent(AppSelected(selectedApp))
}
}
class MainActivityViewModel {
suspend fun submitAppSelection(app: App) { //make this suspend
if (!selectionsCanBeMade()) return
lastSelectedApp = app
//val coroutineScope = CoroutineScope(Dispatchers.Default)
//coroutineScope.launch { appsStartupFsm.submitEvent(AppSelected(app)) }
// different strategy:
// 1. explicitly move off Main thread because we **must have** parallelism (for some reason?)
// 2. synchronize on the result, only after `submitEvent` returns (not suspends) will `submitAppSelection` return.
withContext(Dispatchers.Default) {
appsStartupFsm.submitEvent(AppSelected(app))
}
}
}
@Groostav
Copy link
Author

Also worth noting, with a statement like lastSelectedApp = app, you might want to require that you're on the MainThread, surrounding the whole function with withContext(Dispatchers.Main), because if you dont you've got funny thread safety issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment