我是node和sinon
上的新手,无法测试下面的组件。我想检查res.status
和res.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();
});
});
答案 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.])