我使用 firebase 3.3.0,我想使用 signInWithEmailAndPassword在我的 mocha 单元测试中运行,但出现错误 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"
}
最佳答案
当你说,“我想在我的 mocha 单元测试中使用 signInWithEmailAndPassword 函数” 那么我会告诉你,“为什么”?
您是否正在尝试通过测试他们的身份验证服务是否有效来帮助 Firebase 团队?你真好,但如果你想测试你的应用程序,那么你应该不在单元测试中完全调用 firebase。您真正想要检查的是,您的应用程序在响应返回后运行的代码中是否正确处理了类似于 Firebase 响应的响应。
如果我的任务是为此编写测试,我会使用带有 mocha 的 sinon 库并创建一个 stub 来调用不同的函数,该函数返回一些数据而不是实际调用 Firebase:
这说明了 sinon stub 的语法:
var stub = sinon.stub(object, "method", func);
这就是我在您的示例中要做的:
var stub = sinon.stub(FIREBASE_API_REF.auth(), "signInWithEmailAndPassword"
, () => {
// Simply return a JSON object that is similar to the normal response from Firebase
return {
name: "Jim",
data: {
something: "some stuff"
}
});
关于javascript - Firebase 应用的 Mocha 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069340/