这是我正在测试的文件:
file.js:
function handleRequest(grpcRequestObj, callback){
if(grpcRequestObj.type === REAL_CLIENT_REQUEST){
log.trace("Запрос от реального клиента", refId);
module.exports.realClientRequestWay(grpcRequestObj, callback);
return;
}
}
function realClientRequestWay(grpcRequestObj, callback){
//some logic
}
module.exports = {
handleRequest,
realClientRequestWay
}
测试文件:
test("should call real clients way", ()=>{
jest.mock("../lib/grpc/state");
const state = require("../lib/grpc/state");
state.ready = true;
const constants = require("../lib/grpc/constants");
const handleRequestModule = require("../lib/grpc/handleRequest");
const { handleRequest } = jest.requireActual("../lib/grpc/handleRequest");
handleRequestModule.realClientRequestWay = jest.fn();
const grpcRequest = {
type: constants.REAL_CLIENT_REQUEST,
refId: "123"
};
const mockCb = jest.fn();
handleRequest(grpcRequest, mockCb);
expect(handleRequestModule.realClientRequestWay).toHaveBeenCalledTimes(1);
});
我将realClientRequestWay功能模拟为一个开玩笑的模拟功能,只是为了检查其是否运行,但它不是在打电话,我在做什么错?
您可以使用jest.spyOn(object, methodName)和mockImplementation
用模拟的功能覆盖原始功能。