如何使用sinon在回调中测试回调?

问题描述 投票:1回答:1

我想让我的代码达到100%覆盖率。

现在,我缺少的是这一行:

(err, result) => this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch)

 (accounttype) => globUtil.getConfig(accounttype, constant.BOOK,
            source => this.generateData(req, res, stopwatch, className, methodName, result, source))

我该如何继续这样做?

我试着像:

getIdSpy.callsFake(() => {
    sinon.assert.callCount(processDBResultSpy, 4);      
});

但它总是会回归成功,我知道我的processDBResultSpy没有被召唤4次,只有一次。

代码:

exports.getBookInfo = (req, res) => {

    let stopwatch = globUtil.startStopWatch();

    const METHOD_NAME = "getBookInfo";

    const id = req.params.id;
    const params = [id];

    if (globUtil.isId(id)) {
        bookDao.getBookById(params,
            (err, result) => this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch));

    } else {
        bookDao.getBookBySelfId(params,
            (err, result) => this.processDBResult(err, result, id, req, res, CLASS_NAME, METHOD_NAME, stopwatch));
    }
};

 exports.processDBResult = (err, result, id, req, res, className, methodName, stopwatch) => {

    if (err) {
        log.error(err);

        this.generateErrorResponse(req, res, 500, err, stopwatch, className, methodName);

        return;
    }

    if (result.length == 0) {
        log.info("No results");
        this.generateErrorResponse(req, res, 404, format(message.NOT_FOUND, id), stopwatch, className, methodName)

        return;
    }

    globUtil.getAccountType(id,
        (accounttype) => globUtil.getConfig(accounttype, constant.BOOK,
            source => this.generateData(req, res, stopwatch, className, methodName, result, source)));
}

测试用例:

it('1. getBookInfo - called with id', (done) => {
        let getIdSpy = sinon.stub(bookDao, "getBookById");
        let getSelfIdSpy = sinon.stub(bookDao, "getBookBySelfId");
        let processDBResultSpy = sinon.stub(controller, "processDBResult");

        let request = httpMocks.createRequest({
            method: 'GET',
            url: constant.MODULE_URL,
            params: {
                id: 'BOOKID'
            }
        });

        let response = httpMocks.createResponse();
        controller.getHdbInfo(request, response);

        sinon.assert.callCount(getIdSpy, 1);
        sinon.assert.callCount(getSelfIdSpy, 0);

        //how do i proceed form here

        done();
    });
javascript node.js unit-testing callback sinon
1个回答
0
投票

终于得到了答案。我做的是我产生我的方法。

it('1. getBookInfo - called with id', (done) => {
        let getIdSpy = sinon.stub(bookDao, "getBookById").yields(null, []);
        let getSelfIdSpy = sinon.stub(bookDao, "getBookBySelfId");
        let processDBResultSpy = sinon.stub(controller, "processDBResult");

        let request = httpMocks.createRequest({
            method: 'GET',
            url: constant.MODULE_URL,
            params: {
                id: 'BOOKID'
            }
        });

        let response = httpMocks.createResponse();
        controller.getHdbInfo(request, response);

        sinon.assert.callCount(getIdSpy, 1);
        sinon.assert.callCount(getSelfIdSpy, 0);

        //how do i proceed form here
        sinon.assert.callCount(processDBResultSpy, 1);

        done();
    });
© www.soinside.com 2019 - 2024. All rights reserved.