伙计,这个火力基地单元测试真的在踢我的屁股。
我已经浏览了文档并阅读了它们提供的示例,并测试了一些更基本的 Firebase 函数单元,但我不断遇到问题,我不确定如何验证传递给 refs.transaction
transactionUpdated
函数是否正确更新了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.');
});
});
单元测试代码:
const chai = require('chai');
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const assert = chai.assert;
const sinon = require('sinon');
describe('Cloud Functions', () => {
let myFunctions, functions;
before(() => {
functions = require('firebase-functions');
myFunctions = require('../count.js');
});
describe('countlikechange', () => {
it('should increase /posts/{postid}/likes/likes_count', () => {
const event = {
// DeltaSnapshot(app: firebase.app.App, adminApp: firebase.app.App, data: any, delta: any, path?: string);
data: new functions.database.DeltaSnapshot(null, null, null, true)
}
const startingValue = 11
const expectedValue = 12
// Below code is misunderstood piece. How do I pass along `startingValue` to the callback param of transaction
// in the `countlikechange` function, and spy on the return value to assert that it is equal to `expectedValue`?
// `yield` is almost definitely not the right thing to do, but I'm not quite sure where to go.
// How can I go about "spying" on the result of a stub,
// since the stub replaces the original function?
// I suspect that `sinon.spy()` has something to do with the answer, but when I try to pass along `sinon.spy()` as the yields arg, i get errors and the `spy.firstCall` is always null.
const transactionStub = sinon.stub().yields(startingValue).returns(Promise.resolve(true))
const childStub = sinon.stub().withArgs('likes_count').returns({
transaction: transactionStub
})
const refStub = sinon.stub().returns({ parent: { child: childStub }})
Object.defineProperty(event.data, 'ref', { get: refStub })
assert.eventually.equals(myFunctions.countlikechange(event), true)
})
})
})
我用我的问题注释了上面的源代码,但我会在这里重申一下。
如何验证传递给事务存根的transactionUpdate
回调是否会采用我的startingValue
并将其更改为expectedValue
,然后允许我观察该更改并断言它发生了。
这可能是一个非常简单的问题,有一个明显的解决方案,但我对测试 JS 代码非常陌生,其中所有内容都必须存根,所以这是一个学习曲线......任何帮助,不胜感激。
我同意Firebase生态系统中的单元测试并不像我们希望的那么容易。团队意识到了这一点,我们正在努力让事情变得更好!幸运的是,现在有一些很好的前进方法!
我建议看看我们刚刚发布的这个云函数演示。在这个例子中,我们使用 TypeScript,但这也可以在 JavaScript 中工作。
在src
目录中,您会注意到我们将逻辑拆分为三个文件:index.ts
具有入口逻辑,saythat.ts
具有我们的主要业务逻辑,db.ts
是围绕Firebase实时数据库的薄抽象层。我们只对saythat.ts
进行单元测试;我们有意保持index.ts
和db.ts
非常简单。
在spec
目录中,我们有单元测试;看看index.spec.ts
。您正在寻找的技巧:我们使用mock-require
来模拟整个src/db.ts
文件并将其替换为spec/fake-db.ts
.我们现在不是写入真实的数据库,而是将执行的操作存储在内存中,我们的单元测试可以检查它们看起来是否正确。一个具体的例子是我们的score
字段,它在事务中更新。通过模拟数据库,我们的单元测试来检查是否正确完成的是一行代码。
我希望这对您进行测试有所帮助!