android - 如何修复找不到类的定义:'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>()) }
}
解决方案
您需要在androidContext()
方法中提供应用程序上下文。
由于您的问题是专门针对测试的,因此在我的情况下,我必须通过 ApplicationProvider 传递应用程序上下文:
startKoin {
androidContext(ApplicationProvider.getApplicationContext())
.....
}
不要忘记添加 androidx 依赖项:
testImplementation 'androidx.test:core:1.0.0'
推荐阅读
- c++ - SendMessage 发送的消息在其他类层次结构中未收到
- jquery - Rails 6:根据第一个列表中的选择呈现新列表
- r - predict() 出错,R 不会从 MuMIn 调用 predict.averaging()
- python - 如何在基于 Intel Xeon 处理器的 PC 上安装 Tensorflow?
- amazon-web-services - Cloudformation 为 Application Load Balancer 自动创建证书
- javascript - 我如何在本机反应中访问组件的内部属性(标题)
- r - 在R中解压缩文件时如何解决没有文件错误?
- javascript - 在 array.prototype.map(在 RxJS 管道中)中返回副本或操作原始对象之间的区别
- c# - 按 Linq EF Core 中的项目类别分组
- excel - 将特定单词从 Word 获取到 Excel