我正在尝试使用 Koin 创建一个简单的白盒测试。在设置限定符以将模拟作为参数传递给实例(或假设我想要做的)后,我收到一条错误消息:
org.koin.core.error.NoBeanDefFoundException:没有找到 qualifier='null' & class='class com.imagebucket.main.repository.GalleryRepositoryImpl 的定义(Kotlin 反射不可用)'
测试
class GalleryRepositoryTest: AutoCloseKoinTest() {
private val module = module {
named("repository").apply {
single<GalleryRepository>(this) { GalleryRepositoryImpl() }
single { GalleryViewModel(get(this.javaClass)) }
}
}
private val repository: GalleryRepositoryImpl by inject()
private val vm: GalleryViewModel by inject()
@Before
fun setup() {
startKoin { loadKoinModules(module) }
declareMock<GalleryRepositoryImpl>()
}
@Test
fun uploadImage_withEmptyUri_shouldDoNothing() {
vm.delete("")
verify(repository).delete("")
}
}
视图模型
class GalleryViewModel(private val repository: GalleryRepository) : ViewModel() {
fun delete(name: String) {
repository.delete(name)
}
}
我也尝试了与Robolectric
runner 类似的方法,但是在Application
文件中创建模块后,我的模拟将不会被调用verify(repository).delete("")
。
我怎样才能设法解决这个问题并继续这个简单的测试?