如何用 sinon 存根 require('firebase-admin').auth().getUserByEmail()?
How to stub require('firebase-admin').auth().getUserByEmail() with sinon?
能否分享一些用于 firebase-admin 身份验证的 Sinon 存根示例。挑战在于为进一步的存根初始化 firebase 管理应用程序。
我尝试了下一个代码
const admin = require('firebase-admin');
sinon.stub(admin, 'initializeApp');
var noUserError = new Error('error');
noUserError.code = 'auth/user-not-found';
sinon.stub(admin, 'auth').returns({
getUserByEmail: sinon.fake.rejects(noUserError)
});
var err = await admin.auth().getUserByEmail(email);
console.error(err);
但是 returns
Error (FirebaseAppError) {
codePrefix: 'app',
errorInfo: {
code: 'app/no-app',
message: 'The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.',
},
message: 'The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.',
}
预期结果是异常错误代码 = 'auth/user-not-found'
使用 firebase-mock 有帮助。
https://github.com/soumak77/firebase-mock
const firebasemock = require('firebase-mock');
const mockauth = new firebasemock.MockAuthentication();
const mockdatabase = new firebasemock.MockFirebase();
const mocksdk = new firebasemock.MockFirebaseSdk(
(path) => {
return path ? mockdatabase.child(path) : mockdatabase;
},
() => {
return mockauth;
}
);
mocksdk.auth().autoFlush();
proxyquire('../index', {
'firebase-admin': mocksdk
});
由于 admin.auth
是一个 getter,你不能将它作为一个函数存根,而是作为一个 getter,returns 一个函数与你的存根对象。
您需要使用来自 Sinon API.
的 stub.get(getterFn)
sinon.stub(admin, 'auth').get(() => () => ({
getUserByEmail: sinon.fake.rejects(noUserError)
}));
能否分享一些用于 firebase-admin 身份验证的 Sinon 存根示例。挑战在于为进一步的存根初始化 firebase 管理应用程序。
我尝试了下一个代码
const admin = require('firebase-admin');
sinon.stub(admin, 'initializeApp');
var noUserError = new Error('error');
noUserError.code = 'auth/user-not-found';
sinon.stub(admin, 'auth').returns({
getUserByEmail: sinon.fake.rejects(noUserError)
});
var err = await admin.auth().getUserByEmail(email);
console.error(err);
但是 returns
Error (FirebaseAppError) {
codePrefix: 'app',
errorInfo: {
code: 'app/no-app',
message: 'The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.',
},
message: 'The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.',
}
预期结果是异常错误代码 = 'auth/user-not-found'
使用 firebase-mock 有帮助。
https://github.com/soumak77/firebase-mock
const firebasemock = require('firebase-mock');
const mockauth = new firebasemock.MockAuthentication();
const mockdatabase = new firebasemock.MockFirebase();
const mocksdk = new firebasemock.MockFirebaseSdk(
(path) => {
return path ? mockdatabase.child(path) : mockdatabase;
},
() => {
return mockauth;
}
);
mocksdk.auth().autoFlush();
proxyquire('../index', {
'firebase-admin': mocksdk
});
由于 admin.auth
是一个 getter,你不能将它作为一个函数存根,而是作为一个 getter,returns 一个函数与你的存根对象。
您需要使用来自 Sinon API.
stub.get(getterFn)
sinon.stub(admin, 'auth').get(() => () => ({
getUserByEmail: sinon.fake.rejects(noUserError)
}));