Skip to content

Instantly share code, notes, and snippets.

@CDRussell
Created April 12, 2019 00:26
Show Gist options
  • Save CDRussell/2686c2cfd0df828a0566cc1519c6d291 to your computer and use it in GitHub Desktop.
Save CDRussell/2686c2cfd0df828a0566cc1519c6d291 to your computer and use it in GitHub Desktop.
class ExampleViewModel(private val logger: Logger) : ViewModel() {
@VisibleForTesting
var job: Job? = null
fun handleError() {
job = viewModelScope.launch(Dispatchers.IO) {
heavyOperation()
logger.logErrorEvent()
}
}
}
class ExampleViewModelTest {
private val logger: Logger = mock()
private val testee: ExampleViewModel = ExampleViewModel(logger)
@Test
fun test() = runBlocking {
testee.handleError()
testee.job?.join()
verify(logger).logErrorEvent()
}
}
@CDRussell
Copy link
Author

Test passes

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