Firebase 应用程序中 Compose 的仪器测试
我试图在撰写中创建测试,但总是收到以下错误:
默认 FirebaseApp 在此过程中未初始化 X. 确保首先调用 FirebaseApp.initializeApp(Context)。
我试图最大限度地简化测试代码:
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun test() {
composeTestRule.setContent {
Text("Whatever")
}
}
但仍然出现该错误。
我尝试过使用 uiThreadTestRule:
@get:Rule
val composeTestRule = createComposeRule()
@get:Rule
var uiThreadTestRule: UiThreadTestRule = UiThreadTestRule()
@Test
fun test() {
uiThreadTestRule.runOnUiThread {
FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
}
composeTestRule.setContent {
Text("Whatever")
}
}
以及相同的 composeTestRule.runOnUiThread:
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun test() {
composeTestRule.runOnUiThread {
FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
}
composeTestRule.setContent {
Text("Whatever")
}
}
如何修复 FirebaseApp is not initialized
错误以便可以测试此应用程序的可组合项?
更新
这似乎与 createComposeRule() 相关,导致以下测试:
@get:Rule
val composeTestRule: ComposeContentTestRule = createComposeRule()
@Test
fun test() {
assertEquals(2, 1 + 1)
}
也失败,如果我删除规则,它就会通过。 如果可以删除它,我不需要在测试中使用 firebase 依赖项。
I'm trying to create tests in compose and I'm always getting the following error:
Default FirebaseApp is not initialized in this process X. Make sure to call FirebaseApp.initializeApp(Context) first.
I've tried to simplify the most the code of the test:
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun test() {
composeTestRule.setContent {
Text("Whatever")
}
}
but still getting that error.
I've tried with uiThreadTestRule:
@get:Rule
val composeTestRule = createComposeRule()
@get:Rule
var uiThreadTestRule: UiThreadTestRule = UiThreadTestRule()
@Test
fun test() {
uiThreadTestRule.runOnUiThread {
FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
}
composeTestRule.setContent {
Text("Whatever")
}
}
and also with the same composeTestRule.runOnUiThread:
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun test() {
composeTestRule.runOnUiThread {
FirebaseApp.initializeApp(InstrumentationRegistry.getInstrumentation().targetContext)
}
composeTestRule.setContent {
Text("Whatever")
}
}
How can that FirebaseApp is not initialized
error be fixed so that the composables of this app can be tested?
Update
It seems to be something about the createComposeRule() cause the following test:
@get:Rule
val composeTestRule: ComposeContentTestRule = createComposeRule()
@Test
fun test() {
assertEquals(2, 1 + 1)
}
also fails and it passes if I remove the rule.
I don't need the firebase dependency in the tests if it's possible to remove it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其添加到该模块的 gradle 文件中以排除 firebase 依赖项:
Add this into the gradle file of that module to exclude the firebase dependency:
在 Kotlin DSL 中你可以设置
In Kotlin DSL you can set