gpt4 book ai didi

node.js - 断言错误: expeced to be called once but was called 0 times

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:58 25 4
gpt4 key购买 nike

我正在使用nodejs和sinon。

当前,当我运行我的应用程序(即 UpdateTask 类)时,它工作正常,甚至出现错误。

但是,当我开始进行单元测试时,我遇到了以下问题。

AssertError: expected updateBook to be called once but was called 0 times

我不明白为什么它应该被调用 0 次,而它应该被调用一次。

我的代码有什么问题吗?

更新任务类:

function updateInfo() {

let updateCountParams = [];
let updateParams = [];

let idsToUpdateList = null;

tempTable.getBookForUpdateCount(updateCountParams, function (results) {

if (results[0].RECCOUNT > 0) {

tempTable.getBookForUpdate(updateParams, function (results) {

idsToUpdateList = results;

for (var i = 0; i < idsToUpdateList.length; i++) {
let id = idsToUpdateList[i].id;

let param = [];
param.push(id);

let request = api.sendRequest(id);

// Invoke asynchronous call
request
.buffer(true)
.end(function (err, res) {

if (err) {

tempTable.updateBook(param, function (updateBookResult) {

});

return console.error(err.status + " - " + err.message);
}

let data = {
body: res.body,
text: res.text
};

let bkData = data.text;

if (bkData == undefined || bkData == null) {

tempTable.updateBook(param, function (updateBookResult) {

});

return console.error("DATA NOT FOUND".red);
}

//success flow business logic here
...


}); //end asynchronous call
}
});
}
else {
//no record to be processed.
return;
}
});
}

测试用例:

    describe('Update Task', () => { 
beforeEach(() => {

});

afterEach(() => {
sinon.restore();
});


it('3. API Call - Errror: 404 - Not found', (done) => {

let getTempTableForUpdateCountSpy = sinon.stub(TempTableDao, "getBookForUpdateCount").yields(jsonResult.count.success.result);
let getTempTableForUpdateSpy = sinon.stub(TempTableDao, "getBookForUpdate").yields(jsonResult.single.failure.result);
let getTempTableUpdateSpy = sinon.stub(TempTableDao, "updateBook");

let test = nock('https://test.api.com/id')
.get('/ID125125/')
.reply(404, {

});

updateTask.updateInfo();

sinon.assert.calledOnce(getTempTableForUpdateCountSpy);
sinon.assert.calledOnce(getTempTableForUpdateSpy);
test.interceptors[0].statusCode.should.be.equal(404);
sinon.assert.calledOnce(getTempTableUpdateSpy);

done();
});

最佳答案

问题

tempTable.updateBook 在回调期间调用,但在 sinon.assert.usedOnce(getTempTableUpdateSpy); 运行并失败时尚未运行。

<小时/>

解决方案

确保调用 tempTable.updateBook 的回调在断言之前有机会运行。

当使用 Promise 时,这要容易得多,可以在测试中返回并等待 Promise。这种情况比较棘手,因为存在回调并且没有干净的方法来返回可以等待的内容。

需要注意的重要一点是,测试将保持事件状态,直到超时或调用 done

在这种情况下,看起来 updateBook 是代码中最后发生的事情,也是需要测试的最后一件事。对于这样的场景,可以为 stub 和断言提供模拟实现,然后在模拟实现中调用done

这是一个简化的示例:

import * as sinon from 'sinon';

const tempTable = {
updateBook: () => {}
};

const updateInfo = () => {
setTimeout(() => { tempTable.updateBook(); }, 0); // simulate an asynchronous callback
}

test('updateInfo', (done) => {
const spy = sinon.stub(tempTable, 'updateBook');
spy.callsFake(() => {
sinon.assert.calledOnce(spy); // SUCCESS
done();
});
updateInfo();
});

在你的情况下,你可以这样做:

it('3. API Call - Errror: 404 - Not found', (done) => {

let getTempTableForUpdateCountSpy = sinon.stub(TempTableDao, "getBookForUpdateCount").yields(jsonResult.count.success.result);
let getTempTableForUpdateSpy = sinon.stub(TempTableDao, "getBookForUpdate").yields(jsonResult.single.failure.result);
let getTempTableUpdateSpy = sinon.stub(TempTableDao, "updateBook");

let test = nock('https://test.api.com/id')
.get('/ID125125/')
.reply(404, {

});

getTempTableUpdateSpy.callsFake(() => {
sinon.assert.calledOnce(getTempTableForUpdateCountSpy);
sinon.assert.calledOnce(getTempTableForUpdateSpy);
test.interceptors[0].statusCode.should.be.equal(404);
sinon.assert.calledOnce(getTempTableUpdateSpy);
done();
});

updateTask.updateInfo();
});

关于node.js - 断言错误: expeced to be called once but was called 0 times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53220383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com