Mockgo is a in-memory mocking engine for mongodb. In contrast to existing solutions mockgo does not try to imitate the mongodb interface by implementing its methods. Instead it uses the same approach Mockgoose uses and spins up an actual mongodb instance which holds the data in memory. That way the full feature set of mongodb can be used.
Works on all platforms which is due to the awesome mongodb-prebuilt package.
After spinning up the mongodb instance a connection to that instance is automatically created and returned.
npm install mockgo
Require mockgo, then retrieve a connection to the in-memory instance of mongodb by calling getConnection
.
An optional database name can be specified.
var mockgo = require('mockgo')
mockgo.getConnection((error, connection) => {
//`connection` is the connection to the mongodb instance
})
You can retrieve connections to as many databases as you wish. They are internally cached.
var mockgo = require('mockgo')
mockgo.getConnection('testDatabase', (error, connection) => {
var collection = connection.collection('testDataCollection')
collection.find({}).toArray((error, result) => {
console.log(result); //result: []
collection.insertOne({test: 'data'}, (error, result) => {
collection.find({test: 'data'}).toArray((error, result) => {
console.log(result); //result: [ { _id: 56f52afef6d8838417df1688, test: 'data' } ]
mockgo.shutDown(() => console.log('shutdown complete'))
})
})
})
})
Returns a connection to a in-memory instance on mongodb.
If no databaseName
is specified a dummy name will be used.
If a connection to the same database is requested multiple times a cached version of the same connection instance is returned.
The connection instance is a instance from the official native mongodb driver.
If you wish to use another version of the mongodb package you can easily override it by setting the mongodb
property before requiring a connection:
var mockgo = require('mockgo')
var mockgo.mongodb = require('mongodb') //version xyz
mockgo.getConnection((error, result) => {
// ...
})
Closes all existing mongodb connections and shuts down the mongodb instance.
Exposes the version of the official native mongodb driver, gives the possibility to override it.
This is an example for a simple test with mockgo
in mocha.
var expect = require('chai').expect
var mockgo = require('mockgo')
var Loader = require('../lib/Loader')
describe('loaderTest', () => {
var result, error
before(done => {
mockgo.getConnection((error, connection) => {
expect(error).to.be.null
loader = new Loader(connection)
loader.loadSomething((_error, _result) => {
error = _error
result = _result
done()
})
})
})
after(done => mockgo.shutdown(done))
it('should not return a error', () => expect(error).to.be.null)
it('should load something', () => expect(result).to.deep.equal({awesome: 'data'})
})
The MIT License (MIT)
Copyright (c) 2016 Manuel Ernst
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.