unit-testing - 使用 Jest 与 MQTT 进行单元测试
问题描述
我正在使用 Jest 和 MQTT 编写 API 测试用例。我想用我的期望值验证消息的响应值。目前,我无法处理函数 client.on 的响应。请帮我解决一下,非常感谢
测试.ts
async doTest() {
client.on('connect', async function () {
await client.subscribe(topic + '/reply', function (err) {
if (!err) {
client.publish(topic, JSON.stringify(payload));
}
});
});
await client.on('message', async function (topic, message) {
// message is Buffer
const res = await JSON.parse(message.toString());
const { err, response } = res;
this.result = JSON.stringify(response);
});
}
async getResult() {
return this.result;
}
测试规范.ts
it('should call api', async () => {
await test.doTest();
result = await test.getResult();
console.log(result);
});
解决方案
推荐阅读
- mysql - 有什么方法可以禁用特定 mysql 数据库的区分大小写选项,直到下次重新启动?
- google-apps-script - 使用 getRangeList 导入范围
- r - 在 IF 语句中,以组数和每组观察数为条件
- swift - 核心数据中的 IN 子句 - Swift
- json - 如何在 Flutter 中发出 API 请求
- java - Spring Data JDBC:复合域类型的转换
- java - Spring 应用程序无法重新启动
- spring-amqp - SimpleMessageListenerContainer 和 IllegalArgumentException
- mysql - 获取行排名而不获取每一行
- rust - catch_unwind 信号 SIGABRT 当在其中展开结果时