首页 > 解决方案 > Sinon spy 和 mailgun:试图将未定义的属性“消息”包装为函数

问题描述

我正在尝试使用 Sinon 测试 MailGun 发送方法,调用时出现错误

sandbox.spy(sender, "messages");

错误消息是:TypeError: Attempted to wrap undefined property message as function

请参阅下面的代码,不确定我的错误在哪里。

import * as sinon from 'sinon';
import * as mailgun from 'mailgun-js';
import { EmailgunService } from './emailgun.service';

let sandbox = sinon.createSandbox();
const mailgunSendSpy = sandbox.stub().yields(null, { message: 'hello', id: '100' });

describe('sendEmail method', () => {
    let sender: any;
    beforeEach(() => {
        sender = sandbox.stub(mailgun.default({ apiKey: 'foo', domain: 'bar' }), 'messages')
        .returns({
            send: mailgunSendSpy,
        });
    });

    afterEach(() => {
        sandbox.restore();
    });

    it.only('should call send', async () => {
        sandbox.spy(sender, "messages");
        const emailgunService: EmailgunService = new EmailgunService();
        const result = await emailgunService.sendEmail('test@test.com','test','Hello!');
 
    });
});

我正在使用 mailgun-js 0.22.0 和 sinon 9.2.2

标签: typescriptsinonmailgunsinon-chai

解决方案


推荐阅读