我在 index.js 中有以下函数代码:
var admin = require('firebase-admin');
admin.initializeApp();
admin.storage();
和下面的测试代码:
var assert = require('assert');
var sinon = require('sinon');
describe('Event Sourcing', function() {
var myFunctions, adminInitStub, adminStorageStub,admin;
before(() => {
admin = require('firebase-admin');
adminStorageStub = sinon.stub(admin, 'storage');
adminInitStub = sinon.stub(admin, 'initializeApp');
myFunctions = require('../index');
});
after(() => {
// Restoring our stubs to the original methods.
adminInitStub.restore();
adminStorageStub.restore();
});
describe('CREATED', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,5,3].indexOf(4));
});
});
});
由于某些奇怪的原因,我不断收到以下错误 Error: The default Firebase app does not exist。确保在使用任何 Firebase 服务之前调用 initializeApp()。
但我已经清楚地标记了它。为什么 stub 适用于 initializeApp() 但不适用于 storage()。
谢谢!
最佳答案
admin.storage()
实际上是一个 getter,因此需要这样 stub
adminStorageStub = sinon.stub(admin, 'storage').get(() => {
return () => {
return {bucket: (() => {})};
}
});
可以在此处找到有关 sinon stub 的更多信息 http://sinonjs.org/releases/v2.0.0/stubs/
关于Firebase 函数 : storage cannot be stubbed when writing unit tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631782/