typescript - 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
解决方案
推荐阅读
- c - 在 intel x86 上为裸机开发编写 C 函数,链接器报告溢出
- excel - 获取运行时错误“-2147418113 (8000ffff)”
- awk - 使用 awk/sed 更改列
- unity3d - 实例化 GameObjects 并在它撞到地面时更改材质
- html - CSS object-fit 拉伸图像
- redis - 使用redis作为我的核心BD时我的业务逻辑放在哪里
- arrays - 如何从开关中获取多个值
- html - “文本下划线偏移”在“显示:块”中不起作用
- swift - 'NSButton' 类型的值没有成员 'Clicked' | 带有按钮的 Xcode 错误
- reactjs - React/Redux - 向一个组件添加排序或过滤功能