sinon.mock()。expects()。atLeast()...期望.verify()无法正常工作

时间:2018-10-10 14:26:25

标签: javascript node.js sinon sinon-chai

我是node和sinon上的新手,无法测试下面的组件。我想检查res.statusres.send是否在组件内部被调用。

要测试的组件

module.exports = {

   handle: function(promise, res, next, okHttpStatus) {
       promise
           .then(payload => res.status(okHttpStatus ? okHttpStatus : 200).send(payload))
           .catch(exception => next(exception));
    }
};

单元测试

const sinon = require("sinon");
const routerPromiseHandler = 
require("../../../main/node/handler/PromiseHandler");

describe("Should handle promisse", () => {

    it("should handle success promise return", () => {

        const successMessage = {message: "Success"};

        const promiseTest = new Promise((resolve, reject) => {
            resolve(successMessage);
        });

        let res = {
            status: function() {},
            send: function() {}
        };

        const mockRes = sinon.mock(res);
        const expectStatus =  mockRes.expects("status").withExactArgs(200).atLeast(1)
        const expectSend =  mockRes.expects("send").withExactArgs(successMessage).atLeast(1)

        const spyNext = sinon.spy();

        routerPromiseHandler.handle(promiseTest, res, spyNext, 200);

        expectStatus.verify();
        expectSend.verify();

    });
});

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。正弦检查不起作用,因为间谍在诺言中被称为间谍。检查间谍是否被召唤。我必须在then内添加断言并兑现承诺。

from keras import backend as K

func = K.function(model.inputs + [K.learning_phase()], model.outputs)

# to use it pass 1 to set the learning phase to training mode
outputs = func([input_arrays] + [1.])