![](/img/trans.png)
[英]Koin Android: org.koin.error.NoBeanDefFoundException
[英]Android Test Koin NoBeanDefFoundException
我正在尝试使用 Koin 进行一些 Android 测试,但到目前为止,还没有成功。
我想用 Koin 注入的 ViewModel 测试基本活动。
我已经阅读了类似NoBeanDefFoundException 和 Mock ViewModel 的帖子,用 Koin、Espresso 进行测试,但到目前为止我仍然有错误。
这是与测试配置相关的代码
一个没有模块的特定应用程序。
class MyTestApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin { emptyList<Module>() }
}
}
使用测试应用程序的特定运行程序
class OccazioTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, MyTestApplication::class.java.name, context)
}
}
这是在我的应用程序build.gradle
定义的用作跑步者
android {
defaultConfig {
testInstrumentationRunner "fr.dsquad.occazio.occazio.OccazioTestRunner"
}
}
现在我想测试的代码
在我的MyActivity
class MyActivity : AppCompatActivity(R.layout.activity_my) {
private val myViewModel by viewModel<MyViewModel>()
// Some code
}
和视图模型
class MyViewModel(private val useCase: MyUseCase): ViewModel() {
// Some code
}
最后,测试本身(在 androidTest 中)
@LargeTest
class MyActivityTest : KoinTest {
private lateinit var mockUseCase: MyUseCase
@JvmField
@Rule
val activityRule = activityScenarioRule<MyActivity>()
@Before
fun setup() {
mockUseCase = mock(MyUseCase::class.java)
startKoin {
modules(module { viewModel { MyViewModel(mockUseCase) } })
}
// I've also tried this
loadKoinModules(
module { viewModel { MyViewModel(mockUseCase) } }
)
}
@After
fun cleanUp() {
stopKoin()
}
@Test
fun someTest() = runBlocking {
// Mock the usecase response
`when`(mockUseCase.doSomething()).thenReturn("taratata")
// Start the scenario
val scenario = activityRule.scenario
// Verify we call the getUserId
// Activity is supposed to call the view model that will call the method doSomethingAdterThat.
verify(mockUseCase, times(1)).doSomethingAfterThat()
return@runBlocking
}
}
到目前为止,每次运行此代码时都会出现此错误
org.koin.core.error.NoBeanDefFoundException:
No definition found for 'mypackage.MyViewModel' has been found. Check your module definitions.
有趣的是,当
ActivityTestRule(SplashScreenActivity::class.java, true, false)
更改规则activityScenarioRule
val scenario = activityRule.scenario
更改为val scenario = activityRule.launchActivity(null)
setUp
使用loadKoinModules
而不是startKoin
发生两件事
所以我在这里实际上有两个问题。
activityScenarioRule
工作?好吧,不要问我它是如何工作的,但我想通了。
首先,因为我需要配置,所以我遵循了这个https://medium.com/stepstone-tech/better-tests-with-androidxs-activityscenario-in-kotlin-part-1-6a6376b713ea 。
我做了3件事
首先,我需要在启动前配置 koin,为此,我需要使用ActivityScenario.launch()
和我之前定义的意图
private val intent = Intent(ApplicationProvider.getApplicationContext(), MyActivity::class.java)
var activityRule : ActivityScenario<MyActivity>? = null
// And then I can start my activity calling
activityRule = ActivityScenario.launch(intent)
然后“KoinApp 没有启动”……我只是用 setUp 中的startKoin
替换了loadKoinModules
块
startKoin { modules(module { viewModel { MyViewModel(mockUseCase) } }) }
最后,它适用于 1 个测试,但其他测试失败,因为没有调用像stopKoin()
这样的“KoinAppAlreadyStartedException”。 所以我发现我应该扩展AutoCloseKoinTest
而不是KoinTest
.. 但没有成功。 最后,我已经把stopKoin()
的前startKoin
而现在,一切工作就像一个魅力。
这是我的完整代码
@LargeTest
class MyActivityTest : KoinTest() {
private val intent = Intent(ApplicationProvider.getApplicationContext(), MyActivity::class.java)
var activityRule : ActivityScenario<MyActivity>? = null
private lateinit var mockUseCase: MyUseCase
@Before
fun setup() {
mockUseCase = mock(MyUseCase::class.java)
stopKoin()
startKoin {
modules(module { viewModel { MyViewModel(mockUseCase) } })
}
}
@After
fun cleanUp() {
activityRule?.close()
}
@Test
fun someTest() = runBlocking {
// Mock the usecase response
`when`(mockUseCase.doSomething()).thenReturn("taratata")
// Start the rule
val activityRule = ActivityScenario.launch(intent)
// Verify we call the getUserId
// Activity is supposed to call the view model that will call the method doSomethingAdterThat.
verify(mockUseCase, times(1)).doSomethingAfterThat()
return@runBlocking
}
}
何,我也将此代码添加到我的两个Applications
override fun onTerminate() {
super.onTerminate()
stopKoin()
}
只是要确定 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.