No definition found for qualifier: 如何解决 Kotlin reflection is not available when creating modules with Koin test?

No definition found for qualifier: How to solve Kotlin reflection is not available when creating modules with Koin test?

我正在尝试使用 Koin 创建一个简单的白盒测试。在设置限定符以将模拟作为参数传递给实例(或者我想做的事情)后,我收到一条错误消息:

org.koin.core.error.NoBeanDefFoundException: No definition found for qualifier='null' & class='class com.imagebucket.main.repository.GalleryRepositoryImpl (Kotlin reflection is not available)'

测试

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("")
    }
}

ViewModel

class GalleryViewModel(private val repository: GalleryRepository) : ViewModel() {

    fun delete(name: String) {
        repository.delete(name)
    }
}

我也用 Robolectric runner 尝试了类似的方法,但是在 Application 文件中创建模块后,我的模拟不会使用 verify(repository).delete("").[=16= 调用]

我怎样才能解决这个问题并继续进行这个简单的测试?

正如 Arnaud 在 Koin's repository, issue #584 注意到的那样,应该有一个代码替换

来自:

private val module = module {
    single<GalleryRepository>(named("repository")) {
        GalleryRepositoryImpl() 
    }

    viewModel { GalleryViewModel(get()) }
}

private val repository: GalleryRepositoryImpl by inject()

private val vm: GalleryViewModel by inject(named("repository"))

至:

private val module = module {
    single<GalleryRepository>(named("repository")) { 
        GalleryRepositoryImpl() 
    }

    viewModel { GalleryViewModel(get(named("repository"))) }
}

private val repository: GalleryRepository by inject(named("repository"))

private val vm: GalleryViewModel by inject()