What is MockMongoose?
MockMongoose provides test database by spinning up mongod on the back when mongoose.connect call is made. By default it is using in memory store which does not have persistence.
Install
To install the latest official version, use NPM:
npm install mock-mongoose --save-dev
Usage
You simply require Mongoose and MockMongoose and wrap Mongoose with MockMongoose.
var mongoose = ;var MockMongoose = MockMongoose;var mockMongoose = mongoose; mockMongoose;
Once Mongoose has been wrapped by MockMongoose connect() will be intercepted by MockMongoose so that no MongoDB instance is created.
Mocha
Default mocha timeout is 2000ms, change it to two minutes.
mocha --timeout 120000
Same can be done by creating 'mocha.opts' file in your test directory with "--timeout 120000" entry.
Example
var Mongoose = Mongoose;var mongoose = ; var MockMongoose = MockMongoose;var mockMongoose = mongoose; ; ;
ES6 Example without mocha
;; let mockMongoose: MockMongoose = mongoose; mockMongoose;
Helper methods and variables (mockMongoose.helper)
reset(callback)
Reset method will remove ALL of the collections from a temporary store, note that this method is part of mock-mongoose object, and not defined under mongoose
mockMongoosehelper;
isMocked
Returns TRUE from mongoose object if MockMongoose is applied
if mockMongoosehelper === true // mongoose object is mocked
setDbVersion(mongoDBVersion: string)
Set version of MongoDB release
;; let mockMongoose: MockMongoose = mongoose;mockMongoosehelper; mockMongoose;
setProxy(proxy: string)
Set proxy for downloading MongoDB release
;; let mockMongoose: MockMongoose = mongoose; let proxy: string = processenvhttp_proxy || 'http://example.com:8080';mockMongoosehelper; mockMongoose;
Development
This section contains instructions for developers working on the MockMongoose codebase. It is not relevant if you just want to use MockMongoose as a library in your project.
Pre-requisites
- Node.js >= 4
Setup
git clone git@github.com:CSTLeagueORG/MockMongoose.gitcd MockMongoosenpm installnpm test