【发布时间】:2020-07-17 02:47:42
【问题描述】:
我正在围绕HttpsCallable 云函数设计一个后端 api(适用于 android 和 iOS 应用程序)。通过应用程序测试它们变得非常麻烦,所以我希望使用firebase-functions-test 工具切换到对功能进行单元测试(在生产部署之前)。我一直在关注这个unit testing tutorial。
我在在线模式下运行单元测试时遇到了一些问题。让我提供一些细节。
这是我的 `package.json' 内容:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"test": "mocha --reporter spec"
},
"engines": {
"node": "8"
},
"dependencies": {
"@google-cloud/tasks": "^1.9.0",
"@googlemaps/google-maps-services-js": "^2.0.2",
"chai": "^4.2.0",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.3.0",
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.1.7",
"mocha": "^7.1.1"
},
"private": true
}
我正在使用来自 firebase 后端的 google API(Directions、Geocoding 等),因此为了能够在运行测试时访问它们,我将测试配置在test/index.test.js 在单元测试教程中推荐如下:
const firebaseConfig = {
apiKey: ...,
authDomain: ...,
databaseURL: "https://my-project.firebaseio.com",
projectId: "my-project",
storageBucket: "my-project.appspot.com",
messagingSenderId: ********,
appId: *********
};
const test = require('firebase-functions-test')(firebaseConfig
, 'path_to_json_keyfile/myproject.json');
以下是示例测试代码。请注意,我所有的可调用函数都返回HttpsError,下面的测试只检查HttpsError 的code 字段,如果是ok,则测试通过。我正在测试的函数驻留在一个单独的 rides.js 文件中,index.js 导出为 exports.rides = require(./rides.js)(下面未显示)
const chai = require('chai');
const assert = chai.assert;
describe('Cloud functions', () => {
let rideFunctions;
before(() => {
rideFunctions = require('../index.js');
});
after(() => {
test.cleanup();
});
describe('getRideEstimate', () => {
it('should return ok', async () => {
data = {
riderType: 1,
pickup_lat: 37.0,
pickup_lng: -122,
dropoff_lat: 37.01,
dropoff_lng: -122.01,
scheduledTime: 1585939000,
ts: 1585939000,
tz_id: "uslax"
}
context = {
auth: {
uid: AUTH_ID_FOR_USER_ACCOUNT
}
};
const wrappedGetRideEstimate = test.wrap(rideFunctions.rides.getRideEstimate);
let httpsError = await wrappedGetRideEstimate(data, context);
return assert.equal(httpsError.code, 'ok');
});
})
describe('testCallable', () => {
it('should return ok', async () => {
data = {}
context = {}
const wrappedTestCallable = test.wrap(rideFunctions.rides.testCallable);
let httpsError = await wrappedTestCallable(data, context);
return assert.equal(httpsError.code, 'ok');
})
})
})
问题
- 我的简单
testCallable表单函数
exports.testCallable = functions.https.onCall((data, context) => {
console.log('testCallable');
return new functions.https.HttpsError('ok', '', '');
})
通过了测试(如预期),但莫名其妙地,它似乎在离线模式下运行,因为在 Firebase 控制台的云功能日志中没有记录。此外,如果我禁用笔记本电脑的连接,测试结果保持不变,表明此功能以某种方式在离线模式下运行。
我的
getRideEstimate函数调用了 googleDirectionsAPI,返回一个冗长的400错误,表明在形成对 Directions API 的请求时存在一些问题。我不知道这个错误是否与第一个问题有关,但由于 Directions API 调用嵌入到getRideEstimate函数的更深处,它确实表明该函数正在被调用,但不知何故 API 调用失败了。还有其他方法可以在线测试HttpsCallable函数吗?
【问题讨论】:
标签: javascript firebase google-cloud-functions firebase-tools google-directions-api