Я использовал тест функций firebase, чтобы провести тестирование своих функций.У меня есть некоторый код, который должен отправить что-то в firestore, в основном так же, как это показывают примеры в примерах базы данных в реальном времени:
exports.addMessage = functions.https.onRequest((req, res) => {
const original = req.query.text;
admin.firestore()
.collection('messages')
.add({ original })
.then(documentReference => res.send(documentReference))
.catch(error => res.send(error));
});
Для моего теста я подделал некоторые основныефункциональность с использованием Sinon, мокко и чай.Вот мой текущий тест, который не проходит с сообщением об ошибке: TypeError: firestoreService.snapshot_ is not a function
describe('addMessage', () => {
// add message should add a message to the database
let oldDatabase;
before(() => {
// Save the old database method so it can be restored after the test.
oldDatabase = admin.firestore;
});
after(() => {
// Restoring admin.database() to the original method.
admin.firestore = oldDatabase;
});
it('should return the correct data', (done) => {
// create stubs
const refStub = sinon.stub();
// create a fake request object
const req = {
query : {
text: 'fly you fools!'
}
};
const snap = test.firestore.makeDocumentSnapshot({ original: req.query.text }, 'messages/1234');
// create a fake document reference
const fakeDocRef = snap._ref;
// create a fake response object
const res = {
send: returnedDocRef => {
// test the result
assert.equal(returnedDocRef, fakeDocRef);
done();
}
};
// spoof firestore
const adminStub = sinon.stub(admin, 'firestore').get(() => () => {
return {
collection: () => {
return {
add: (data) => {
const secondSnap = test.firestore.makeDocumentSnapshot(data, 'messages/1234');
const anotherFakeDocRef = secondSnap._ref;
return Promise.resolve(anotherFakeDocRef);
}
}
}
}
});
// call the function to execute the test above
myFunctions.addMessage(req, res);
});
});
Мой вопрос, как, черт возьми, я могу это исправить?
У меня ранее был тест, который былЯ просто прошел первый снимок и fakeDocRef, и мой тест прошел успешно, но как только я выполню обещание с новой ссылкой на поддельный документ, он не будет выполнен ...
Любая помощь будет принята!Спасибо!