我想测试一个与我的firebase实时数据库的连接。你能做这个吗?如何连接到我的数据库并获取快照,然后对返回的数据进行断言?我没有找到通过引用做很多事情的例子,甚至只是验证真正的连接。
我正在使用v1.0.0的firebase函数, 摩卡,打字稿,柴。
// Test framework.
import 'mocha';
// Assertion library.
import { use, expect, assert } from 'chai';
// Standalone test spies, stubs and mocks.
import * as sinon from 'sinon';
// Extends Chai with assertions for the Sinon.JS
import * as sinonChai from 'sinon-chai';
use(sinonChai);
const admin = require('firebase-admin');
const test = require('firebase-functions-test')({
databaseURL: 'https://<myappname>.firebaseio.com',
storageBucket: '<myappname>.appspot.com',
projectId: '<myappname>',
}, './<myappname>-firebase-adminsdk-9pvzn-9999999999.json');
admin.initializeApp();
describe('Suite: Connection to Firebase Database', () => {
let index;
before(() => {
// Require index.js and save the exports inside a namespace called index.
// I am not testing any functions here...just a connection to the database.
});
after(() => {
// Do cleanup tasks.
test.cleanup();
});
it('should connect to firebase and make sure there is data', () => {
const ref = admin.database().ref('/organizations/1235/yieldTag/');
return ref.orderByChild('pickDateTime')
.startAt(100)
.endAt(100000000000)
.once('value')
.then(snapshot => {
expect(snapshot).is.not.null; // <-- this passes
expect(snapshot.ref.path.pieces_[0]).is.equal('organizations'); // <-- my ref shows up here
console.log('exists?: ' + snapshot.exists()); // <-- snapshot never "exists."
if (snapshot.exists()) {
expect(snapshot.length).is.greaterThan(0);
}
else {
console.log('cn: snapshot not provided'); // <-- my code falls here.
}
}).catch((error) => {
console.log('there was an error in "should connect to firebase and make sure there is data." ' + error);
});
});
});
这是我的package.json
{
"name": "functions",
"version": "0.0.0",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"test": "mocha -r ts-node/register tests/**/*.spec.ts",
"deployDev": "tsc && firebase deploy --project dev --only functions",
"deployProd": "tsc && firebase deploy --project prod --only functions",
"lint": "tslint -p tsconfig.json src/**/*.ts -e tests/**/*.ts"
},
"description": "Cloud Functions for Firebase",
"dependencies": {
"@types/firebase": "^2.4.32",
"cors": "^2.8.4",
"fecha": "^2.3.3",
"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.2",
"json2typescript": "^1.0.5",
"lodash": "^4.17.10",
"superagent": "^3.8.3"
},
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/cors": "^2.8.4",
"@types/mocha": "^5.2.0",
"@types/sinon": "^4.3.1",
"@types/sinon-chai": "^2.7.29",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"concurrently": "^3.5.1",
"firebase-functions-test": "^0.1.2",
"mocha": "^5.1.1",
"rimraf": "^2.6.2",
"rollup": "^0.58.2",
"rollup-plugin-node-resolve": "^3.3.0",
"sinon": "^4.5.0",
"sinon-chai": "^3.0.0",
"ts-node": "^6.0.2",
"tslint": "^5.10.0",
"typescript": "^2.8.3"
},
"private": true
}