如何检查是否已在嘲笑中调用了模拟的内部函数?

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

这是我正在测试的文件:

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功能模拟为一个开玩笑的模拟功能,只是为了检查其是否运行,但它不是在打电话,我在做什么错?

javascript node.js unit-testing jestjs jest
1个回答
0
投票

您可以使用jest.spyOn(object, methodName)mockImplementation用模拟的功能覆盖原始功能。

© www.soinside.com 2019 - 2024. All rights reserved.