/monckoose

Mock your mongoose entities !

Primary LanguageJavaScript

What's monckoose?

Monckoose helps you working with mongoose but without any database. It provides a fake driver and a collection helper to make it easy to code any entity mocks. No database needed, no connection needed, just a js file with your entities.

Installation

$ npm install monckoose

Usage

First you need to put this line before any require of 'mongoose' :

require('monckoose');

Because it sets a global variable to tell mongoose to use the monckoose driver instead of the native one.

Then add the mocks location to options :

mongoose.connect('mongodb://localhost/mocks', { mocks: require(path.join(__dirname, 'myMocks')) });

Create a mocks module in myMocks.js, for example :

var monckoose = require('monckoose')
  , mocks, contacts;

contacts = [
  { name: 'Robert', company: 'fake', email: 'robert@fake.net', _id: '51605fe1779ade6334000009', __v: 0 },
  { name: 'Jennifer', company: 'fake', email: 'jennifer@fake.net', _id: '51605fe1779ade6334000004', __v: 0 }
];

mocks = {
  contacts: new monckoose.MonckooseCollection(contacts)
};

module.exports = exports = mocks;

That's all! Now you can work as usual with mongoose, for example :

var Contact = mongoose.model('Contact', {
  name: String,
  company: String,
  email: String
});
var john = new Contact({ name: 'John', company: 'woo', email: 'john@woo.org' });
john.save(function (err) {
  ...
});

Limitations

Currently available methods are limited to : find, findOne, insert, remove, save, update, findAndModify. Contributions are welcome to complete MonckooseCollection prototype.

See unit tests in spec for more examples

Enjoy !