【问题标题】:How fix No definition found for class:'android.content.Context'. Check your definitions! in KoinTest如何修复找不到类的定义:'android.content.Context'。检查你的定义!在 KoinTest
【发布时间】:2020-06-17 12:38:16
【问题描述】:

在我的 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>()) }
}

【问题讨论】:

    标签: android koin


    【解决方案1】:

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

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

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

    别忘了添加androidx依赖:

    testImplementation 'androidx.test:core:1.0.0'

    【讨论】:

    • 你能不能用get&lt;Context&gt;()代替androidContext()
    • 新错误:无法解析上下文实例。请在您的 KoinApplication 配置中使用 androidContext() 函数。
    • 您是否在测试类的startKoin{} 块中添加了androidContext(ApplicationProvider.getApplicationContext())
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多