[英]How to send context parameter in unit test to a firebase functions running on the functions emulator
I manage to start the firebase emulators and load a cloud function.我设法启动了 firebase 模拟器并加载了一个云功能。 Now I want to write a test.
现在我想写一个测试。
PROBLEM I use chai-http
to call the function in the emulator but I do not succeed in sending a context
to the function.问题我使用
chai-http
在模拟器中调用函数,但我没有成功地向函数发送context
。
when calling the function using chai-http
, I see the following warning and error :使用
chai-http
调用函数时,我看到以下警告和错误:
{"severity":"WARNING","message":"Request body has extra fields: context"}
{"severity":"ERROR","message":"Invalid request, unable to process."}
{"severity":"WARNING","message":"Request body has extra fields: context"}
{"severity":"ERROR","message":"Invalid request, unable to process."}
Here is the test code snippet :这是测试代码片段:
it("function_call", async() => {
await new Promise((resolve, reject) => {
chai.request(url)
.post("")
.send({
data: {
id: FILE_ID
},
context: {
auth: {
uid: USER_ID,
token: USER_TOKEN
}
}
})
.end(function(err, res) {
console.info(JSON.stringify(res));
const payload = JSON.parse(res.text);
chai.expect(payload.error).not.null;
resolve();
});
});
// expect some data from firestore emulator to be deleted
const afterCAll = await firestore.collection(`users/${USER_ID}/files/${FILE_ID}`).get();
chai.expect(afterCAll.empty).is.true;
});
And here is the function code :这是功能代码:
export const doSomething = async(data, context) => {
console.log("context=" + JSON.stringify(context));
console.log("context.auth=" + JSON.stringify(context.auth));
}
export const CLOUD_FUNCTION = functions
.runWith(runtimeOpts)
.region("REGION")
.https
.onCall(doSomething);
And to execute the test, i run :为了执行测试,我运行:
firebase emulators:exec --project dev-export 'npm test --prefix functions --verbose --debug'
Leaving out the context in the parameter :省略参数中的上下文:
chai.request(url)
.post("")
.send({
data: {
id: FILE_ID
}
})
and the function call to the emulator works just fine并且对模拟器的函数调用工作正常
Looking for similar cases with the error you provided, I have a found a question that tries to get user credentials from context, and I noticed the functions they were using were not asynchronous, you might want to check context
, and authData
.寻找与您提供的错误类似的情况,我发现了一个试图从上下文中获取用户凭据的问题,我注意到他们使用的函数不是异步的,您可能想要检查
context
和authData
。 You can try:你可以试试:
exports.doSomething = functions.https.onCall((data, context) => {
const result = context;
const result= context.auth;
console.log("context=" + JSON.stringify(result));
console.log("context.auth=" + JSON.stringify(resultAuth));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.