Lukasz Kalnik, similar to the CoroutineDispatcher, CoroutineScope needs to be injected into the ViewModel (VM) for testing.

The viewModelScope is created within the VM. Therefore, the CoroutineScope is only passed as an argument for test VMs, otherwise the default argument is set to null, and the scope provider extension function created uses the VM's viewModelScope.

Here is how I injected CoroutineScope. I recommend Craig Russell’s post for injecting the CoroutineDispatcher.

--

--

Open Sourcer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Hurwitz

Adam Hurwitz

Open Sourcer

More from Medium

Capturing full sized camera images the right way in android: Fileprovider

Google I/O 22: Android Keynote

Taming Android Unit Test when using different flavors and build types

Watch files on folder with Kotlin