如何在MongoDB上模拟“错误"事件 [英] How to simulate an 'error' event on MongoDB
问题描述
我正在尝试为NoFlo组件(由同事编写)编写测试用例-该组件具有连接" inPort和错误" outPort,例如:
I'm trying to write a test case for a NoFlo component (written by a colleague) - where the component has a "connect" inPort and an "error" outPort like:
var self = this; // a NoFlo Component
var mongodb = null;
self.inPorts.connect.on("data", function(uri) {
mongodb = mongojs(uri);
self.outPorts.connected.send(mongodb);
mongodb.on("error", function(error) {
self.outPorts.error.send(error);
});
});
因此,基于此代码模式,我应该如何模拟错误情况(在测试用例中),以便它通过outPort发送错误?
So based on this code pattern, how should I simulate an erroneous situation (in the test case) so that it sends an error through the outPort?
我尝试发送错误的uri,例如"lcalhost:99999/abcdef",但它不起作用.
I tried sending a bad uri like "lcalhost:99999/abcdef", but it doesn't work.
更新:原始代码通过已连接" outPort发送mongodb
实例,我将其缓存以成功发出错误"事件.
Update: the original code sends the mongodb
instance through a "connected" outPort, I cached it to emit the "error" event successfully.
推荐答案
mongojs
扩展了EventEmitter
.
假设mongodb
是全局的,则调用mongodb.emit("error", "This is an error")
激活error
事件.
Assuming mongodb
is global, call mongodb.emit("error", "This is an error")
to activate the error
event.
如果未定义mongodb
,请尝试使用self
的值来访问它.
If mongodb
is undefined, try using the value of self
to get access to it.
更多信息:
- http://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2
- https://github.com/mafintosh/mongojs/blob/master/index.js
- http://cjohansen.no/talks/2011/xp-meetup/# 70
- http://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2
- https://github.com/mafintosh/mongojs/blob/master/index.js
- http://cjohansen.no/talks/2011/xp-meetup/#70
这篇关于如何在MongoDB上模拟“错误"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!