首页 > 解决方案 > 如何修复找不到类的定义:'android.content.Context'。检查你的定义!在 KoinTest

问题描述

在我的 KoinTest 中扩展

@Before
    fun setUp() {
        startKoin { modules(KoinStarter.getModules()) }
    }

@Test
fun `should inject my components`() {
    val settingsStore: SettingsStore = get()
    assertNotNull(settingsStore)
}

我收到错误找不到类的定义:'android.content.Context'。检查你的定义!

但我的模块位于 KoinStarter.getModules()

val localDataModule = module {
    factory<Resources> { get<Context>().resources }
    single<SettingsStore> { SettingsStore(get<Context>()) }
}

标签: androidkoin

解决方案


您需要在androidContext()方法中提供应用程序上下文。

由于您的问题是专门针对测试的,因此在我的情况下,我必须通过 ApplicationProvider 传递应用程序上下文:

startKoin {
    androidContext(ApplicationProvider.getApplicationContext())
    .....
}

不要忘记添加 androidx 依赖项:

testImplementation 'androidx.test:core:1.0.0'


推荐阅读