One way I'm thinking is to use an invalid opereator:
collection.findOne({foo: {'$fooooo': true}}, function(err, item) {
console.log(err);
});
prints:
{ [MongoError: invalid operator: $fooooo] name: 'MongoError' }
Question
I need to test error handling in a node app I'm working on. How can I make Mongo return an error in its callback so I can test error handling in my app?
collection.findOne({a: b}, function(err, item) {
// how can I build a query to make an err?
});
Solution
One way I'm thinking is to use an invalid opereator:
collection.findOne({foo: {'$fooooo': true}}, function(err, item) {
console.log(err);
});
prints:
{ [MongoError: invalid operator: $fooooo] name: 'MongoError' }
OTHER TIPS
In your test, rather than require
ing your actual Mongo driver, you could use a mock dummy driver that returns errors.
Running thisIsNotACollection.findOne({a:b}) in the CLI gives - ReferenceError: thisIsNotACollection is not defined. As long as you never define thisIsNotACollection as a collection you should consistently get an error back.