zak.taccardi
12/19/2018, 9:56 PMzak.taccardi
12/19/2018, 9:56 PMzak.taccardi
12/19/2018, 9:56 PMuiDispatcher : CoroutineDispatcher = Main
zak.taccardi
12/19/2018, 9:56 PMuiDispatcher : CoroutineDispatcher
zak.taccardi
12/19/2018, 9:56 PMzak.taccardi
12/19/2018, 9:56 PMSam
12/19/2018, 9:57 PMzak.taccardi
12/19/2018, 9:57 PMdispatcher : CoroutineContext
Sam
12/19/2018, 9:57 PMstreetsofboston
12/19/2018, 9:57 PMTestCoroutineContext
, which has a Dispatcher that you can control by moving virtual time?Sam
12/19/2018, 9:57 PMprivate val viewModel = MainFragmentViewModel( appsRepository, Dispatchers.Unconfined )
zak.taccardi
12/19/2018, 9:57 PMDispatchers.Default
for my view modelsSam
12/19/2018, 9:58 PMzak.taccardi
12/19/2018, 9:58 PMDispatchers.Main
if you preferSam
12/19/2018, 9:58 PMSam
12/19/2018, 9:58 PMSam
12/19/2018, 9:58 PMrunBlocking {}
Sam
12/19/2018, 9:58 PMzak.taccardi
12/19/2018, 9:59 PMzak.taccardi
12/19/2018, 9:59 PMzak.taccardi
12/19/2018, 9:59 PMSam
12/19/2018, 9:59 PMSam
12/19/2018, 10:00 PMstreetsofboston
12/19/2018, 10:00 PMSam
12/19/2018, 10:00 PMSam
12/19/2018, 10:00 PMsuspend fun updateCurrentInstalledApps( installedPackages : List<String> ) = withContext( IO ) {
appsDao.updateCurrentInstalledApps( installedPackages )
}
streetsofboston
12/19/2018, 10:00 PMSam
12/19/2018, 10:01 PMSam
12/19/2018, 10:01 PM@Test
fun `onDisableAllAppsClicked is relayed to repository`() {
runBlocking {
viewModel.onDisableAllAppsClicked()
verify( appsRepository ).updateAllAppsExcludedStatus( true )
}
}
Sam
12/19/2018, 10:01 PMprivate val appsRepository = mock<AppsRepository>()
private val viewModel = MainFragmentViewModel( appsRepository, Dispatchers.Unconfined )
streetsofboston
12/19/2018, 10:02 PMSam
12/19/2018, 10:03 PMSam
12/19/2018, 10:03 PMstreetsofboston
12/19/2018, 10:04 PM<http://Dispatchers.IO|Dispatchers.IO>
, which will run on a background thread. This one doesn’t do really anything, since your test-function returns almost immediately.streetsofboston
12/19/2018, 10:05 PMMain
and IO
. Either use, for your tests, the Unconfined one or use the TestCoroutineContext for more granular control.Sam
12/19/2018, 10:09 PMSam
12/19/2018, 10:09 PMstreetsofboston
12/19/2018, 10:11 PMupdateAllAppsExcludedStatus
is called or not…
I wonder if the suspend
is not handled well by Mockito, since the actual JVM signature is quire different from when it would have been non-suspend.Sam
12/19/2018, 10:13 PMSam
12/19/2018, 10:13 PMstreetsofboston
12/19/2018, 10:13 PMSam
12/19/2018, 10:13 PMSam
12/19/2018, 10:14 PMtestImplementation 'org.mockito:mockito-core:2.22.0'
testImplementation 'com.nhaarman:mockito-kotlin:1.5.0'
streetsofboston
12/19/2018, 10:14 PMSam
12/19/2018, 10:18 PMSam
12/19/2018, 10:19 PMtestCompile 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0'
Sam
12/19/2018, 10:19 PMSam
12/19/2018, 10:20 PMstreetsofboston
12/19/2018, 10:22 PMdave08
12/20/2018, 3:47 AMSam
12/20/2018, 4:06 AM