russhwolf
06/12/2021, 3:38 PMcheckModules()
in 3.1.0. Nothing in the changelog about it. What’s the story there? I’m having trouble understanding how to migrate my tests from the lambda-based setup I had before.russhwolf
06/12/2021, 3:38 PMtry {
val koinApplication = startKoin { ... }
koinApplication.koin.checkModules {
koin.createScope(testScopeId, koinUiScopeQualifier)
create<LocationDetailViewModel> { parametersOf(1L) }
}
} finally {
stopKoin()
}
I have a custom scope in my koin configuration, so I needed the createScope()
call in the test to mimic what the prod code was doing.russhwolf
06/12/2021, 3:38 PMcheckModules()
that takes a lambda is gone. So I’m trying something like this
try {
val koinApplication = startKoin { ... }
koinApplication.koin.createScope(testScopeId, koinUiScopeQualifier)
koinApplication.checkModules(mapOf(Long::class to 1L))
} finally {
stopKoin()
}
This gives me java.lang.IllegalStateException: Missing MockProvider
, which I think means I’m not wiring something up correctly because I’m not using mocks in my tests, so I assume the error is just because it can’t find something it’s looking for.John O'Reilly
06/12/2021, 4:17 PMMockProviderRule
is needed now...
https://github.com/joreilly/PeopleInSpace/commit/57e2eea1cba8538602f4d28ac55c8c3a2f9084bfrusshwolf
06/12/2021, 5:28 PMJohn O'Reilly
06/12/2021, 5:28 PMJohn O'Reilly
06/12/2021, 5:29 PMapp
module right now....but probably should have it in common code....russhwolf
06/12/2021, 5:34 PMrusshwolf
06/12/2021, 5:35 PMrusshwolf
06/12/2021, 7:13 PMInvalidMutabilityException
when you call register()
.
class Box(val contents: String)
class KoinModulesTest {
@Test
fun checkModulesTest() {
val koinApplication = startKoin {
modules(module {
single { "foo" }
single { Box(get()) }
})
}
koinApplication.checkModules()
}
@AfterTest
fun tearDown() {
stopKoin()
}
}
russhwolf
06/14/2021, 3:05 AMarnaud.giuliani
06/14/2021, 8:06 AMcheckModules
is simplified, following the new internalsarnaud.giuliani
06/14/2021, 8:07 AMarnaud.giuliani
06/14/2021, 4:27 PMarnaud.giuliani
06/14/2021, 4:32 PMarnaud.giuliani
06/14/2021, 4:32 PM/**
* Check all definition's dependencies - start all modules and check if definitions can run
*/
fun KoinApplication.checkModules(
defaultValues: Map<KClass<*>, Any> = hashMapOf(),
allowedMocks: List<KClass<*>> = listOf(),
allowedExceptions: List<KClass<*>> = listOf()
) = koin.checkModules(defaultValues, allowedMocks, allowedExceptions)
arnaud.giuliani
06/14/2021, 4:33 PMcheckModules(
allowedMocks = listOf(
WorkerParameters::class,
SavedStateHandle::class
),
allowedExceptions = listOf(
NullPointerException::class
)
){
androidContext(MockProvider.makeMock<Application>())
modules(allModules)
}
russhwolf
06/14/2021, 6:21 PMMockProvider
arnaud.giuliani
06/15/2021, 6:51 AM