我正在尝试使用async await/sinon,但出现了一个奇怪的错误,我得到的是:
演员表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);
}
});
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.