簡體   English   中英

Node / Mocha / Chai / Sinon-異步等待單元測試錯誤

[英]Node / Mocha / Chai / Sinon - Async await unit test error

我正在嘗試使用async 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正在捕獲期望的錯誤,這是輸出:

    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.

如果我不檢查預期的失敗(最后一項,retailer3的總和為10),則代碼可以正常工作,如果我刪除了函數中的陷阱(無論如何我都無法在實時代碼中完成),那么mocha不會似乎仍未調用done()。

您不應該混合使用回調和async/await ,即使已傳遞了回調, exports.getUnbilledChargesSummary也會返回promise。

要在調用函數中獲得異步函數的結果,可以使用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;
    }
};

為了在Mocha中測試異步功能,您可能需要將回調調用為已done或返回承諾。 因此,作為getBillableRetailerListgetAllUnbilledChargesSums是異步功能也一樣,你應該使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM