如何在MongoDB上模拟“错误"事件 [英] How to simulate an 'error' event on MongoDB

查看:93
本文介绍了如何在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

这篇关于如何在MongoDB上模拟“错误"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆