【发布时间】:2020-06-28 16:39:52
【问题描述】:
我正在尝试使用 declareMock 和 mockk 模拟存储库类,但它似乎无法正常工作,因为我正在从真实存储库获取数据。
版本
implementation "org.koin:koin-androidx-viewmodel:2.1.6"
testImplementation "org.koin:koin-test:2.1.6"
代码
我的应用程序类(仅相关部分):
class MyApplication : Application() {
companion object {
val appModule = module {
single<RepositoryUserLists> { RepositoryUserListsImpl() }
}
}
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@MyApplication)
modules(appModule)
}
}
}
我的测试课(仅相关部分)
class TestRepositoryUserLists : KoinTest {
@get:Rule
val koinTestRule = KoinTestRule.create {
modules(MyApplication.appModule)
}
@get:Rule
val mockProvider = MockProviderRule.create { clazz ->
mockkClass(clazz.java.kotlin)
}
@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()
private val repo: RepositoryUserLists by inject()
@Before
fun before() {
declareMock<RepositoryUserLists> {
every { getAllLists() } returns MutableLiveData(listOf(MyList("test list")))
}
//PROBLEM IS HERE
//Expected: a list containing one item names "test list".
//Actual: empty list (like in real repository).
repo.getAllLists().value
}
}
【问题讨论】:
标签: android unit-testing koin mockk