男人,这个firebase单元测试真的踢我的屁股.
我已经阅读了文档并阅读了他们提供的示例,并且已经对我的一些基本的Firebase功能单元进行了测试,但是我一直遇到问题,我不知道如何验证transactionUpdated函数是否传递到refs .transaction正确更新current对象.
我的斗争可能最好用他们的child-count示例代码和我在为其编写单元测试时做出的不良尝试来说明.
假设我想要进行单元测试的函数执行以下操作(直接从上面的链接中获取):
// count.js
exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite(event => {
const collectionRef = event.data.ref.parent;
const countRef = collectionRef.parent.child('likes_count');
// ANNOTATION: I want to verify the `current` value is incremented
return countRef.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (current || 0) + 1;
}
else if (!event.data.exists() && event.data.previous.exists()) {
return (current || 0) - 1;
}
}).then(() => {
console.log('Counter updated.');
});
});
Run Code Online (Sandbox Code Playgroud)
单元测试代码:
const chai …Run Code Online (Sandbox Code Playgroud) unit-testing sinon firebase sinon-chai google-cloud-functions