我使用firebase 3.3.0并且我想在我的mocha单元测试中使用signInWithEmailAndPassword函数,但是我收到错误 auth / network-request-failed
template <int X, typename Y, typename A = int, typename B = int>
struct Z {
constexpr Z() {}
};
template <typename Y, typename A, typename B>
struct Z<2, Y, A, B> {
constexpr Z(int W) { }
};
int main() {
constexpr Z<0, void> z1;
constexpr Z<2, void> z2{42};
}
test.js
Unhandled rejection Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred.
的package.json
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);
})
});
});
答案 0 :(得分:2)
当你说, &#34;我想在我的摩卡单元测试中使用signInWithEmailAndPassword函数&#34; 然后我会对你说,&#34;为什么&#34;
您是否尝试通过测试其身份验证服务来帮助Firebase团队?这对你很好,但是如果你想测试你的应用程序那么你应该不在单元测试中呼叫firebase。您真正想要检查的是,类似于Firebase响应的响应由应用程序在响应返回后运行的代码中正确处理。
如果我的任务是为此编写测试,我将使用带有mocha的sinon库并创建一个调用不同函数的存根,该函数返回一些数据而不是实际调用Firebase:
这说明了一个sinon存根的语法:
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"
}
});
答案 1 :(得分:1)
答案 2 :(得分:1)
对于遇到此问题的其他人,我能够确定来自jsdom的全局var doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = doc.defaultView;
for (var key in window) {
if (!window.hasOwnProperty(key)) continue;
if (key in global) continue;
if (key == 'XMLHttpRequest') continue;
global[key] = window[key];
}
对象的问题。通过使用此代码设置我的全局变量,我能够摆脱错误:
{{1}}