如何在mailgun-js的nodejs应用中用玩笑创建手动模拟?

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

app / src / emails / account.js中的代码

const mailgun = require("mailgun-js");
const DOMAIN = "sxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org";
const mg = mailgun({apiKey: process.env.MAILGUN_API_KEY, domain: DOMAIN});

const sendWelcomeEmail = async (email, name) => {
    const dataForMail = {
        to: email,
        from: '[email protected]',
        subject: 'Testing!',
        text: `Welcome to the app, ${name}, let me know how you get along with the app.`,

    }

    mg.messages().send(dataForMail)

}

app / test / __ mocks __ / mailgun-js中的代码:

module.exports = {
    messages() {

    },
    send() {

    },
    mailgun() {

    }

}

[每当我开玩笑时,它就会说'mailgun不是函数'。如何为该构造函数创建手动模拟?

node.js jestjs mailgun
1个回答
0
投票

我的模拟对象是对象,而不是函数。测试双打需要匹配他们要替换的事物的接口;在这种情况下,它必须是一个函数,该函数使用messages方法返回对象(使用send方法返回对象)。我的模拟完全不符合该结构。 (非常感谢@jonrsharpe将此告知)mailgun-js.js需要以相同的方式进行编辑。

module.exports = function(apiKey, domain) {
    const object2 = {
        send() {

        }
    }

    const object1 = {
        messages () {
            return object2
        }
    }
    return object1
}
© www.soinside.com 2019 - 2024. All rights reserved.