我正在尝试使用异步await / sinon,但得到一个奇怪的错误,这就是我所拥有的:
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
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();
});
});
});
似乎我的函数中的catch正在捕获expect的错误,这是输出:
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.
如果我没有检查失败预期(最后一项,零售商3的总和为10),代码可以正常工作,如果我删除了我的功能中的捕获(我无法做到这一点)无论如何直播代码),摩卡似乎还没有调用done()。
答案 0 :(得分:2)
你不应该混合回调和async/await
,exports.getUnbilledChargesSummary
会返回一个承诺,即使是传递回调。
要在调用函数中获取异步函数的结果,可以使用async/await
或promise chain:
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;
}
};
为了测试Mocha中的异步函数,您可能需要调用回调done
或返回一个promise。因此,getBillableRetailerList
和getAllUnbilledChargesSums
也是异步函数,您应该在sinon.stub中使用resolves
,而不是returns
回调。
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 });
});
});