- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码。
private client: any;
this.client = mqtt.connect(url, mqttOptions);
this.client.on('message', (topic: string, message: string) => {
console.log('came inside onMessage');
let ksiotMessage: KSIOTMessage;
let receivedTopic: KSIOTTopic;
receivedTopic = getValue(topic);
ksiotMessage = {
ksiotTopic: receivedTopic,
payload: message
};
messageReceivedCallBack.onMessageReceived(ksiotMessage);
});
这里有一个匿名函数,当 mqtt 客户端发出消息事件时,该函数会被触发。我无法控制它,这意味着不是我将其绑定(bind)到事件发射器。另外,我无法添加计时器并等待此事件触发,因为我已经使用 sinon 模拟了客户端的连接。所以没有真正的联系。那么我如何手动发出此消息事件并查看是否调用了 messageReceivedCallBack 的 onMessageReceived ?我是 Javascript 单元测试的新手。目前我正在使用 mocha 和 sinon 来满足我的单元测试要求。
请指教。
最佳答案
首先有一个重要的免责声明:我不了解 TypeScript,也不了解您的应用程序的内部结构,因此以下内容可能无法开箱即用。但是,它可能会为您提供一些有关如何实现此类测试的想法。
我抽象了您的客户端代码(需要测试的代码):
// mqtt-module.js
const mqtt = require('mqtt');
const messageReceivedCallBack = require('./callbacks');
// This mimics what your code does. Instead of a class I just make it a
// simple function.
module.exports = {
setup() {
let client = mqtt.connect();
client.on('message', (topic, message) => {
// We want to test this.
messageReceivedCallBack.onMessageReceived(message);
});
}
}
在您的代码中,不清楚 messageReceivedCallBack
来自何处。因为您必须能够从 Sinon 访问它,所以它应该位于可导入模块中(但是,这确实依赖于导入被缓存的事实,就像使用 require()
一样,其中我'我不确定 TS 是否这样做)。
这是我使用的一个简单的模拟:
// callbacks.js
module.exports = {
onMessageReceived(message) {}
};
最后是测试本身。它相当复杂,因为它需要做各种事情:
EventEmitter
子类,用于替换原来的MqttClient
代码:
// test.js
const mqtt = require('mqtt');
// The modules mentioned above.
const mqttModule = require('./mqtt-module');
const messageReceivedCallBack = require('./callbacks');
// Set up Sinon and Chai
const sinon = require('sinon');
const chai = require('chai');
let expect = chai.expect;
chai.use(require('sinon-chai'));
// Create a fake client for testing purposes.
const EventEmitter = require('events').EventEmitter;
class Client extends EventEmitter {}
// The test case.
describe('my test case', () => {
var mockClient;
beforeEach(() => {
mockClient = new Client();
// mqtt.connect() returns a fake client instance, with
// just enough logic to allow events to be emitted and
// received.
sinon.stub(mqtt, 'connect').returns(mockClient);
// Call the setup of our MQTT class (this will likely be
// entirely different in your case, but the idea is that
// it gets called _after_ `mqtt.connect()` gets stubbed.
mqttModule.setup();
});
afterEach(() => {
// Restore the original.
mqtt.connect.restore();
});
it('should call messageReceivedCallBack.onMessageReceived', () => {
// The message that we're going to pass.
let message = 'this is a test message';
// We want to stub messageReceivedCallBack.onMessageReceived()
sinon.stub(messageReceivedCallBack, 'onMessageReceived');
// Emit a `message` event on our mock client, which will trigger
// the `client.on('message', ...)` in your MQTT class.
mockClient.emit('message', 'topic', message);
// Test if the stub was called with the proper argument.
expect(messageReceivedCallBack.onMessageReceived).to.be.calledWith(message);
// Restore to the original function.
messageReceivedCallBack.onMessageReceived.restore();
});
});
关于javascript - 对不受事件发射器绑定(bind)的事件进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37403657/
我是 Arduino 和这个论坛的新手,这是我除了教程之外的第一个 Arduino 项目。 我正在尝试使用遥控发射器/接收器和 Arudino 来控制伺服。我使用 Arduino 而不是将伺服器直接连
我喜欢听一个特定的广播,我用 vlc 给自己做了一个小发射器,就像这样: #!/bin/sh vlc http://mp3lg.tdf-cdn.com/fip/all/fiphautdebit.mp3
是否有用于发出 YAML 的 C++ 库?维基百科提到了 libyaml 的 c++ 包装器,但链接已断开。 YAML官方网站只提供yaml-cpp ,这也在 this 中提出所以问题,但 cpp-y
我正在尝试将我的 iPhone 设置为广播 iBeacon 信号以供另一部 iPhone 接收。本应接收信号的 iPhone 可以正常工作,但我无法让另一部 iPhone 广播信号。 我正在学习本教程
我正在编写一个网络应用程序,其中多个监听器(Evcentsource SSE 客户端 JS)将连接到我的服务器。我想做的是 为每个连接的监听器存储 SSE 发射器:可以在内存中或通过为每个客户端分配
我想在后端进行串行工作。我想选择 geo_unit 并做另一项工作并向客户端写入响应。 我读了 http://nodejs.org/docs/v0.4.7/api/events.html#events
我从早期就使用 BIRT,但仍然有关于 PDF 发射器的谜语。 短篇小说 : 我可以配置 fontsConfig.xml 以从相对路径或 jar 加载字体吗? 长话: 我们同时使用 FOP 和 BIR
今天我得到了一些很酷的 radio 接收器和发射器,问题是当我输入我想发送的消息时,它会在另一个 arduino 上吐出乱码。 //transmitter #include void setup()
我有一个使用 html canvas 函数创建的形状(四分之一圆): 移动到 LineTo QuadraticCurveTo 如何将形状爆炸成粒子,然后将它们返回形成一个圆圈? 最佳答案 我不会为您编
我正在实现自定义 Birt 发射器,我需要知道单元格、表格或网格的宽度和高度(以像素或点为单位)。如果未在报表设计器中明确设置度量,则会出现此问题。 我试图通过调用 cellContent.getWi
我将给出我正在尝试做的事情的一个小前提。我有一个游戏概念,需要多个玩家围坐在一张 table 旁,有点像扑克牌。 不同播放器之间的正常交互,通过socket.io结合node js很容易处理。 我很难
我是一名优秀的程序员,十分优秀!