【发布时间】:2021-02-22 22:05:18
【问题描述】:
如果我尝试使用 Firebase Emulator Suite 在 espresso 测试用例中使用 createUserWithEmailAndPassword(emai, password) 创建一个新用户,则不会发生任何事情。相同的代码适用于实时 Firebase 实例。日志什么也没显示。
我正在尝试使用 Firebase Emulator Suite 来测试我的应用。模拟器正确启动。日志显示:
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4000 │
└─────────────────────────────────────────────────────────────┘
┌────────────────┬──────────────────────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Authentication │ localhost:9099 │ http://localhost:4000/auth │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Database │ localhost:9000 │ http://localhost:4000/database │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Hosting │ Failed to initialize (see above) │ │
├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
│ Pub/Sub │ localhost:8085 │ n/a │
└────────────────┴──────────────────────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
模拟器 UI 也可以正常工作。我正在使用 9.4.0 版中的 Firebase 工具
我的 espresso 测试代码如下:
@Test
fun registerAndSigningInWithVeryNewUser() {
val auth = Firebase.auth
auth.useEmulator("10.0.2.2", 9099) // like https://firebase.google.com/docs/emulator-suite/connect_auth#android_ios_and_web_sdks
// sign out at first to ensure that accidentally no other user is logged in
auth.signOut()
// and now creating new user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "registerAndSigningInWithVeryNewUser: creating new user success")
val user = auth.currentUser
} else {
// If sign in fails
Log.w(TAG, "registerAndSigningInWithVeryNewUser: creating new user failure", task.exception)
assert(false)
}
}
Thread.sleep(10000)
val user = auth.currentUser
Log.d(TAG, "registerAndSigningInWithVeryNewUser: User = $user")
}
我认为原因是异步函数调用,尽管我添加了丑陋的 Thread.sleep(10000) 语句。但没有效果。
如果我在模拟器 UI 中手动创建用户并尝试使用 signInWithEmailAndPassword(email, password) 登录,也会发生同样的情况(什么都没有)
有什么想法吗?
【问题讨论】:
标签: android kotlin firebase-authentication android-espresso firebase-tools