Kenny Ki
Kenny Ki

Reputation: 3430

How to simulate an 'error' event on MongoDB

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);
    });
});

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?

I tried sending a bad uri like "lcalhost:99999/abcdef", but it doesn't work.


Update: the original code sends the mongodb instance through a "connected" outPort, I cached it to emit the "error" event successfully.

Upvotes: 2

Views: 959

Answers (1)

Larry Battle
Larry Battle

Reputation: 9178

mongojs extends EventEmitter.

Assuming mongodb is global, call mongodb.emit("error", "This is an error") to activate the error event.

If mongodb is undefined, try using the value of self to get access to it.

More info:

Upvotes: 3

Related Questions