Я работаю над некоторыми перегибами в моих модульных тестах для приложения node с интеграцией Firebase RTDB. Я пытаюсь создать тесты для моих слушателей Firebase, но один из них сводит меня с ума.
Например, если у меня есть эта функция:
и этот тест:
Этот тест проходит, как и ожидалось. Но если у меня есть эта функция:
С этим тестом:
мой тест завершается с ошибкой AssertionError: ожидаемый шпион был вызван с аргументами {}
.
Что я упускаю?
export function my_function(callback) {
database.ref(`test`).on('child_changed', async snapshot => {
callback(snapshot);
});
};
describe('my_function', () => {
beforeEach(() => {
databaseStub = sinon.stub(database, 'ref').returns({
on: sinon.stub()
});
});
afterEach(() => {
databaseStub.restore();
});
it("should call the callback function on 'child_changed'", () => {
const callback = sinon.spy();
const snapshot = {}; // for clarity; actual data is not needed
databaseStub().on.callsArgWith(1, snapshot);
my_function(callback);
expect(callback).to.have.been.calledWith(snapshot);
});
});
export async function my_function(callback) {
database.ref('test').on('child_added', async snapshot => {
const snap = await database.ref('messages').once('value'); // <- here's the rub
// prints "depots: {}" as expected
console.log(`depots: ${ JSON.stringify(snap) }`);
// calling with a new object, not the one passed in as in the previous example
callback(snap);
});
}
describe('my_function', () => {
beforeEach(() => {
databaseStub = sinon.stub(database, 'ref').returns({
on: sinon.stub(),
once: sinon.stub().returns({}),
update: sinon.stub()
});
});
afterEach(() => {
databaseStub.restore();
});
it("should call the callback function on 'child_added'", () => {
const callback = sinon.spy();
const snapshot = {}; // for clarity; actual data is not needed
const messages = {}; // for clarity; actual data is not needed
databaseStub().on.callsArgWith(1, snapshot);
my_function(callback);
expect(callback).to.have.been.calledWith(messages);
});
});
Остромир
Вопрос задан2 мая 2024 г.