【问题标题】:Testing Firebase Functions gives error "TypeError: Cannot read property 'send' of undefined"测试 Firebase 函数时出现错误“TypeError:无法读取未定义的属性‘发送’”
【发布时间】:2020-01-17 17:13:31
【问题描述】:

我正在使用 Mocha 来测试我的功能。它们既可以部署在 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 functions = require('firebase-functions')
const mongoose = require('mongoose')
const Tag = require('../../models/tag')

const uri = 'MongoDB URL'

module.exports = functions.https.onRequest(async (req, res) => {
    try {
        await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
        const result = await Tag.find({})
        res.send({ message: 'Request fulfilled', success: true, result: result })
    } catch (err) {
        console.log('function:getTags:error: ', err)
        res.send({ message: 'Unable to fulfill the request', success: false, error: err })
    }
})

最后,这是我的测试代码:

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 正在做的事情,我有点困惑

【问题讨论】:

    标签: firebase unit-testing google-cloud-functions mocha.js


    【解决方案1】:

    第一个错误是由于在没有参数的情况下调用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 你会得到同样的错误。

    作为解决方案,我找到了类似测试 here 的示例。这是与 Firebase 相同的 Google Cloud Function 参考。而且我使用了相同的方法并创建了请求和响应参数。更改后 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 变量将始终为undefined。正如您所看到的运行我的示例,它将使用发送响应的函数 res.send。我不太了解 mocha,无法帮助您进行测试,但请查看我上面通过的参考以进行类似测试。

    我希望这能帮助你弄清楚...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-05
      • 2020-08-01
      • 2021-08-02
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 2023-03-09
      • 2021-07-23
      相关资源
      最近更新 更多