javascript (node.js):触发多个事件的单元测试代码
我正在尝试编写单元测试代码(caolan 的nodeunit)来测试多个事件触发。
所以,我正在测试
function A() {
this.fireaway = function() {
send_message_A_to_queue();
send_message_B_to_a_different_queue();
send_message_C_to_a_web_service();
};
}
因此,仅对 1 个事件进行单元测试很容易。
exports.A = function(test) {
a = new A();
queue = new Queue();
queue.on('message', function(err, message) {
test.ok(true, "got message");
test.done();
};
a.fireaway();
};
现在,我正在使用 setTimer() 进行倒计时并强制在 X 时间内完成,但是有更好的方法吗?
I am attempting to write unit test code (caolan's nodeunit) that will test for multiple event firings.
So, I am testing
function A() {
this.fireaway = function() {
send_message_A_to_queue();
send_message_B_to_a_different_queue();
send_message_C_to_a_web_service();
};
}
So, unit testing only 1 event is easy.
exports.A = function(test) {
a = new A();
queue = new Queue();
queue.on('message', function(err, message) {
test.ok(true, "got message");
test.done();
};
a.fireaway();
};
Right now, I'm using setTimer()
to do a count down and force completion by X amount of time, but is there a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试这样的操作:
基本上,我正在跟踪已触发的事件,当我找到所有事件时,发出我自己的事件,因此可以在该事件上调用 test.done() 。
You could try something like this:
Basically, I'm keeping track of the events that have fired, and when I find all of them, emit my own, so test.done() can be called on that event.
一种可能的答案是对每个事件的回调进行编码。
IE:
这对于单次使用实例非常有用,我还没有尝试过多个实例。
回调1
嗯...
One possible answer is to encode callbacks for each event.
IE:
This works great for single usage instances, I haven't tried multiple ones yet.
callback1
urhm...