如何在 Jest 中使用构造函数模拟类中的方法?

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

我正在做这个项目,它有一个类,其构造函数和方法看起来像这样

handleProjectLaunchRefund
退款

    const moxyClub = new moxyTokenomics.MoxyClubSDK(env.FLOW_NETWORK);
    const userWallet = await db.user.findUserWalletById(userId);
    const refundData = await moxyClub.refundPaymentDoneToTreasury(userWallet.address, amount);

我试图通过这样做来模拟

new moxyTokenomics.MoxyClubSDK(env.FLOW_NETWORK);

const mockMoxyClubSDK = jest.fn();
const mockPaymentDoneToTreasury = jest.fn();

jest.mock('moxy-tokenomics', () => {
    return jest.fn().mockImplementation(() => {
        jest.fn().mockImplementation(() => ({
            refundPaymentDoneToTreasury: mockPaymentDoneToTreasury
        }))
    });
});


moxyTokenomics.MoxyClubSDK = mockMoxyClubSDK;

db.user.findUserWalletById.mockReturnValue(
     userValue
);

const result = ProjectService.handleProjectLaunchRefund('id', 20, 20);

但它会抛出这样的错误

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: moxyClub.refundPaymentDoneToTreasury is not a function".] {

如何正确模拟refundPaymentDoneToTreasury

unit-testing jestjs chai
© www.soinside.com 2019 - 2024. All rights reserved.