测试Firebase功能时出现错误“;TypeError:无法读取属性';发送';“未定义”的定义;

测试Firebase功能时出现错误“;TypeError:无法读取属性';发送';“未定义”的定义;,firebase,unit-testing,google-cloud-functions,mocha.js,Firebase,Unit Testing,Google Cloud Functions,Mocha.js,我用摩卡来测试我的功能。它们在Firebase上部署时以及在本地使用仿真器运行时都可以工作 我的问题是,我不断收到指向函数本身的错误: function:getTags:error: TypeError: Cannot read property 'send' of undefined at /Users/garrettlove/development/projects/cirrcl/crcle_backend/functions/categories/tags/getTags.js:

我用摩卡来测试我的功能。它们在Firebase上部署时以及在本地使用仿真器运行时都可以工作

我的问题是,我不断收到指向函数本身的错误:

function:getTags:error:  TypeError: Cannot read property 'send' of undefined
    at /Users/garrettlove/development/projects/cirrcl/crcle_backend/functions/categories/tags/getTags.js:24:7
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:76616) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at /Users/garrettlove/development/projects/cirrcl/crcle_backend/functions/categories/tags/getTags.js:28:7
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:76616) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:76616) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
然后我得到了这个错误指向我的测试:

test:getTags:error:  TypeError: Cannot read property 'should' of undefined
    at Context.<anonymous> (/Users/garrettlove/development/projects/cirrcl/crcle_backend/functions/test/index.test.js:17:11)
最后,这里是我的测试代码:

const admin = require('firebase-admin')
const serviceAccount = require('../../../../../keys/cirrcl-firebase.json');
const getTags = require('../categories/tags/getTags')

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "DB URL"
  });

describe('Describe something here', () => {
    it('Should succeed or fail', async () => {
        try {
            const result = await getTags()
            console.log('getTags test result: ', result)
            result.should.have.status(200)
        } catch (err) {
            console.log('test:getTags:error: ', err)
            // err.should.throw()
        }
    })
})

我能想到的唯一一件事是凭证问题,但这并没有完全意义,因为我有一个正在使用的证书(我也尝试过没有它以及一些文档所说的应用程序引擎证书),另外,由于我可以在本地运行这个程序,这也是我认为mocha正在做的事情,所以我有点困惑

第一个错误是由于调用没有参数的
getTags
。我在节点中复制了如下内容:

test.js:

const functions = require('firebase-functions')

module.exports = functions.https.onRequest((req, res) => {
        res.send()
})
test2.js:

const test = require("./test.js");
const result = test();
console.log(result);
当您运行:
node test2.js
时,您将得到相同的错误

作为解决方案,我找到了相似测试的例子。这是谷歌云函数参考,与Firebase相同。我使用了相同的方法,创建了请求和响应参数。更改后test2.js如下所示:

const test = require("./test.js");
const req = { body: {  name: 'name' }  };
const res = {send: () => console.log("test2")};
test(req, res);
现在,如果再次运行:
node test2.js
,您将不会得到任何错误。因此,我认为您应该将参数添加到
getTags
调用中

当涉及到第二个错误时。函数
onRequest
没有返回任何值,因此
result
变量将始终是
未定义的
。正如您在运行我的示例时所看到的,它将使用函数res.send来发送响应。我不太了解摩卡咖啡,无法帮助您进行测试,但请检查我通过上述类似测试的参考资料

我希望这能帮你找到答案

const test = require("./test.js");
const req = { body: {  name: 'name' }  };
const res = {send: () => console.log("test2")};
test(req, res);