你能分享一些用于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);但它会返回
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‘
发布于 2019-08-26 03:33:07
使用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
});发布于 2019-11-05 16:56:12
由于admin.auth是一个getter,所以您不能将其作为函数存根,而是将其作为一个getter返回带有存根对象的函数。您需要在Sinon API中使用stub.get(getterFn)。
sinon.stub(admin, 'auth').get(() => () => ({
getUserByEmail: sinon.fake.rejects(noUserError)
}));https://stackoverflow.com/questions/57566458
复制相似问题