[英]Firebase sinon / chai unit testing in nodeJS
Trying to get offline unit testing working for my Firebase project.尝试为我的 Firebase 项目进行离线单元测试。 I can't seem to figure out why the following test isn't running and logging console.log('b') & ('c').我似乎无法弄清楚为什么以下测试没有运行并记录 console.log('b') & ('c')。 I assume I've stubbed it incorrectly but can't figure it out.我假设我错误地存根它,但无法弄清楚。 By the test output and console.log it only returns 'a' and then also returns true.通过测试 output 和 console.log 它只返回“a”,然后也返回 true。
Help appreciated.帮助表示赞赏。
index.js
function createFreeAgents(admin) {
admin.database().ref('/CONFIG').update({
"agencyActive" : false,
"listCount" : true
});
**console.log('a')**
admin.database().ref('/FREEAGENTPICK').once('value', snap => {
**console.log('b')**
snap.forEach(team => {
**console.log('c')**
let newAgentObject = {
"rookie": 2,
"senior": 4,
}
admin.database().ref('/FREEAGENTPICK/' + team.key).update(newAgentObject);
})
})
return true;
}
const chai = require('chai');
const expect = chai.expect;
const assert = chai.assert;
const sinon = require('sinon');
const admin = require('firebase-admin');
const test = require('firebase-functions-test')();
const functions = require('../index');
describe('it should update a players score', () => {
let adminInitStub, adminDatabaseStub;
beforeEach(() => {
adminInitStub = sinon.stub(admin, 'initializeApp');
adminDatabaseStub = sinon.stub(admin, 'database');
});
afterEach(() => {
adminInitStub.restore();
test.cleanup();
});
describe('run createFreeAgent()', () => {
it('should run createFreeAgents() and return true', () => {
// Stubs
databaseStub = sinon.stub();
refStub = sinon.stub();
updateStub = sinon.stub();
// Values
configUpdate = {
"agencyActive" : false,
"listCount" : true
};
// DBMocks
adminDatabaseStub = Object.defineProperty(admin, 'database', { get: () => databaseStub });
databaseStub.returns({ ref: refStub });
refStub
.withArgs('/CONFIG')
.onFirstCall()
.returns({ update: updateStub })
.withArgs('/FREEAGENTPICK')
.onFirstCall()
.returns({ once: updateStub })
updateStub
.onFirstCall()
.returns(configUpdate)
.onSecondCall()
.returns(snapFreeAgent)
assert.deepEqual(updateStub('/CONFIG'), configUpdate);
result = functions.createFreeAgents(adminDatabaseStub);
expect(result).to.equal(true);
return;
})
})
})
test output
it should update a players score
run createFreeAgent()
a
√ should run createFreeAgents() and return true
1 passing (13ms)
Disclaimer: I have not test this (because too much dependency).免责声明:我没有对此进行测试(因为依赖太多)。
The idea is: you can not use the same updateStub for method update and once , because they have different implementation ( update only need 1 arg, while once need 2 args).这个想法是:你不能对方法更新和一次使用相同的updateStub ,因为它们有不同的实现(更新只需要 1 个参数,而一次需要 2 个参数)。 You can change your updateStub for once method to simple fake (but if you want to still use stub, you also can), in order to run console.log b and c.您可以将updateStub for once方法更改为简单的伪造(但如果您仍想使用存根,您也可以),以便运行 console.log b 和 c。
Example: implementation using fake示例:使用假实现
const onceFake = sinon.fake((arg1, arg2) => {
// arg1: will contain string 'value' => based on your sample code.
// arg2: will contain function.
// Then you need to call arg2 with injected value.
arg2(configUpdate); // This is where console.log b and c will get called.
});
Do not forget to change .returns({ once: updateStub })
不要忘记更改.returns({ once: updateStub })
with .returns({ once: onceFake })
.使用.returns({ once: onceFake })
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.