Firebase Cloud函数,在单元测试中设置ref父级



我有以下Firebase云功能:

functions.database.ref(`/users/{uid}/subscription`).onWrite((change, context) => {
const uid = context.params.uid
const value = change.after.val()
if (!change.before.exists() && !value) {
return null
}
return change.after.ref.parent?.once('value').then(snapshot => {
const user = snapshot.val()
console.warn(`user`, user)
const email = user?.providerData?.email
if (typeof email !== 'string') {
return null
}
console.log('Update flag at Mailer', context.params.uid, email, fieldname)
return createOrUpdateContact(uid, user)
})
})

我正在尝试为它编写一个测试,使用firebase-functions-test谷歌的框架来模拟Firebase:的后端实现

it('correctly updates flag at mailer', async (done) => {
const userId = chance.guid();
const userEmail = chance.email()
const userAfter = {
uid: userId,
subscription: true,
providerData: {
email: userEmail
}
};
const beforeSnap = functionsMock.database.makeDataSnapshot(
{
uid: userId,
subscription: false,
providerData: {
email: userEmail
}
},
`/users/${userId}`
);
const afterSnap = functionsMock.database.makeDataSnapshot(
true,
`/users/${userId}/subscription`
);
const wrapped = functionsMock.wrap(update_subscription_flag);
const change = functionsMock.makeChange(beforeSnap, afterSnap);
const res = await wrapped(change, {
params: {
uid: userId
}
});
expect(res).toBeTruthy()
expect(createOrUpdateContact).toHaveBeenCalledWith(userId, userAfter)

done()
} )

然而,当测试在这条线上执行时,会出现一个问题:

return change.after.ref.parent?.once('value').then(snapshot => {

它应该给出ref对象的parent(这是订阅标志(,这在部署时有效,但在我的测试代码中,它返回null,因为ref没有父对象,有什么方法可以修复这种行为吗?是否强制设置更改值的父级?

仅供参考:我尝试使用完整的用户对象作为afterSnap,但后来Framework变得更加愚蠢,将after和before对象作为用户对象,而不仅仅是属性。

干杯

为了完整性,如果它对其他人有用,这里是为这个和GitHub上的Firebase函数测试库提出的问题。

  • Firebase函数测试问题
  • GitHub上的Firebase函数测试库

最新更新