【发布时间】:2016-12-28 09:40:42
【问题描述】:
我使用 firebase 3.3.0,我想在我的 mocha 单元测试中使用 signInWithEmailAndPassword 函数,但我收到错误 auth/network-request-failed
Unhandled rejection Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred.
test.js
const FIREBASE_CONFIG = {
apiKey: "AIzaSyDdA1POUWy9eid1AdBYuMdxch_k8ob7Qrg",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
storageBucket: "my-app.appspot.com",
};
const FIREBASE_API_REF = firebase.initializeApp(FIREBASE_CONFIG);
before(function (done) {
promise = new Promise(function (resolve, reject) {
return FIREBASE_API_REF.auth().signInWithEmailAndPassword(firstUserEmail, firstUserPassword)
.then(function (userData) {
firstUserId = userData.uid;
resolve(userData);
done();
}, function (error) {
return reject(error);
})
});
});
package.json
"scripts": {
"test": "mocha --grep ./e2e.js --invert --compilers js:babel-register -R spec --ui bdd --timeout 7000"
}
【问题讨论】:
-
您需要显示
FIREBASE_API_REF所指的内容。此外,代码还有其他问题:done从未被调用;resolve永远不会被调用;而且您不需要创建自己的承诺 - 那是 antipattern。 -
@cartant 感谢您的回复,这不是
before功能的完整部分。我只是想展示一段无法调用signInWithEmailAndPassword函数的代码。请查看我编辑的问题。 -
我认为问题的部分问题在于不清楚您在问什么,并且您已经剪掉了部分测试代码 -
describe和it调用在哪里?此外,您还没有指定如何运行测试:在使用 Karma 的浏览器中;还是在 Node 中使用 mocha 命令? -
@cartant 我使用 mocha 命令运行测试(我将此脚本添加到我的原始帖子中)。如果我收到错误
before函数,describe和it并不重要。我不明白我的问题有什么不清楚的地方。我展示了一段代码,其中我在 firebase 函数中出现错误,我不知道为什么。 -
This Gist 为我工作,可能对您比较有帮助
标签: javascript firebase mocha.js firebase-authentication