[英]Node / Mocha / Chai / Sinon - Async await unit test error
I 'm experimenting with async await / sinon, but getting a weird error, here is what I have: 我正在尝试使用async await / sinon,但是遇到了一个奇怪的错误,这就是我所拥有的:
billing.js billing.js
exports.getUnbilledChargesSummary = async(function (callback) {
try {
var retailerList = await(exports.getBillableRetailerList());
var chargesList = await(exports.getAllUnbilledChargesSums());
var result = exports.assignUnbilledChargesSumForEachRetailer(retailerList, chargesList);
return callback(null, result);
}
catch (ex) {
console.error('Exception in getUnbillecChargesSummary');
console.error(ex)
return callback(ex);
}
});
billing.test.js billing.test.js
describe('billing', () => {
const retailers = [{ id: 111, common_name: 'Retailer 1' }, { id: 222, common_name: 'Retailer 2' }, { id: 333, common_name: 'Retailer 3' }];
const charges = [{ retailer_id: 111, sum: 100 }, { retailer_id: 222, sum: 200 }];
it('should get summary of all unbilled charges for each retailer', (done) => {
var getBillableRetailerListStub = sinon.stub(billing, 'getBillableRetailerList').returns(Promise.resolve(retailers));
var getAllUnbilledChargesSumsStub = sinon.stub(billing, 'getAllUnbilledChargesSums').returns(Promise.resolve(charges));
billing.getUnbilledChargesSummary((err, result) => {
console.log('result', result);
expect(result).to.deep.include({ id: 111, common_name: 'Retailer 1', sum: 100 });
expect(result).to.deep.include({ id: 222, common_name: 'Retailer 2', sum: 200 });
expect(result).to.deep.include({ id: 333, common_name: 'Retailer 3', sum: 10 });
done();
});
});
});
It seems that the catch in my function is catching the error for expect, here is the output: 似乎我函数中的catch正在捕获期望的错误,这是输出:
billing
result [ { id: 111, common_name: 'Retailer 1', sum: 100 },
{ id: 222, common_name: 'Retailer 2', sum: 200 },
{ id: 333, common_name: 'Retailer 3', sum: 0 } ]
Exception in getUnbillecChargesSummary
{ AssertionError: expected [ Array(3) ] to deep include { id: 333, common_name: 'Retailer 3', sum: 10 }
at billing.getUnbilledChargesSummary (/Users/User/work/billing_api/services/billing.test.js:19:36)
at Object.<anonymous> (/Users/User/work/billing_api/services/billing.js:58:16)
at tryBlock (/Users/User/work/billing_api/node_modules/asyncawait/src/async/fiberManager.js:39:33)
at runInFiber (/Users/User/work/billing_api/node_modules/asyncawait/src/async/fiberManager.js:26:9)
message: 'expected [ Array(3) ] to deep include { id: 333, common_name: \'Retailer 3\', sum: 10 }',
showDiff: false,
actual:
[ { id: 111, common_name: 'Retailer 1', sum: 100 },
{ id: 222, common_name: 'Retailer 2', sum: 200 },
{ id: 333, common_name: 'Retailer 3', sum: 0 } ],
expected: undefined }
result undefined
Unhandled rejection AssertionError: Target cannot be null or undefined.
at billing.getUnbilledChargesSummary (/Users/User/work/billing_api/services/billing.test.js:17:36)
at Object.<anonymous> (/Users/User/work/billing_api/services/billing.js:63:16)
at tryBlock (/Users/User/work/billing_api/node_modules/asyncawait/src/async/fiberManager.js:39:33)
at runInFiber (/Users/User/work/billing_api/node_modules/asyncawait/src/async/fiberManager.js:26:9)
1) should get summary of all unblled charges for each retailer
- should get list of all billable retailers
- should get sum for each unbilled retailer in retailer bill charges
0 passing (2s)
5 pending
1 failing
1) billing should get summary of all unbilled charges for each retailer:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
If I don't check the failure expect (last item, retailer3 with sum of 10), the code works without problems, if I remove the catch in my function (which I can't really do in live code anyways), mocha doesn't seem to call done() still. 如果我不检查预期的失败(最后一项,retailer3的总和为10),则代码可以正常工作,如果我删除了函数中的陷阱(无论如何我都无法在实时代码中完成),那么mocha不会似乎仍未调用done()。
You shouldn't mix callbacks and async/await
, exports.getUnbilledChargesSummary
returns a promise, even with passed callback. 您不应该混合使用回调和
async/await
,即使已传递了回调, exports.getUnbilledChargesSummary
也会返回promise。
To get result of async function in calling function, you can use async/await
or promise chain: 要在调用函数中获得异步函数的结果,可以使用
async/await
或promise链:
exports.getUnbilledChargesSummary = async () => {
try {
var retailerList = await(exports.getBillableRetailerList());
var chargesList = await(exports.getAllUnbilledChargesSums());
return exports.assignUnbilledChargesSumForEachRetailer(retailerList, chargesList);
} catch (err) {
console.error('Exception in getUnbillecChargesSummary');
console.error(err);
throw err;
}
};
For testing async functions in Mocha, you can need to call callback done
or return a promise. 为了在Mocha中测试异步功能,您可能需要将回调调用为已
done
或返回承诺。 So as getBillableRetailerList
and getAllUnbilledChargesSums
are async functions too, you should use resolves
, not returns
callback in sinon.stub. 因此,作为
getBillableRetailerList
和getAllUnbilledChargesSums
是异步功能也一样,你应该使用resolves
,没有returns
回调sinon.stub。
describe('billing', () => {
const retailers = [{ id: 111, common_name: 'Retailer 1' }, { id: 222, common_name: 'Retailer 2' }, { id: 333, common_name: 'Retailer 3' }];
const charges = [{ retailer_id: 111, sum: 100 }, { retailer_id: 222, sum: 200 }];
it('should get summary of all unbilled charges for each retailer', async () => {
let getBillableRetailerListStub = sinon.stub(billing, 'getBillableRetailerList').resolves(retailers);
let getAllUnbilledChargesSumsStub = sinon.stub(billing, 'getAllUnbilledChargesSums').resolves(charges);
let result = await billing.getUnbilledChargesSummary();
expect(result).to.deep.include({ id: 111, common_name: 'Retailer 1', sum: 100 });
expect(result).to.deep.include({ id: 222, common_name: 'Retailer 2', sum: 200 });
expect(result).to.deep.include({ id: 333, common_name: 'Retailer 3', sum: 10 });
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.