【发布时间】: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