0

我正在尝试使用 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)
    }
}

我也尝试了与Robolectricrunner 类似的方法,但是在Application文件中创建模块后,我的模拟将不会被调用verify(repository).delete("")

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

4

1 回答 1

1

正如 Arnaud 在Koin的存储库中所注意到的,问题#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()
于 2019-10-10T12:33:05.487 回答