MongoDB helper for Mocha. Useful for seeding data for API tests.
npm install mocha-mongodb --save-dev
var db = require('mocha-mongodb');
describe('some test', function () {
db.connect('mongodb://localhost/test');
db.dropDb();
db.create('users', {
name: 'Name Goes Here'
});
it('works', function () {
// do stuff here
});
});
Description
Connect to the specified database. Runs in a "before" block. Assigns the database connection instance to this.db
. Can take an optional options
object (see below).
Example
db.connect(process.env.MONGO_URL, { lib: 'mongojs' })
before(function () {
this.db.users.find({}, function (err, users) {
// do stuff here
});
});
Options
Alias
add
, create
Description Create a new object in a collection. Runs in a "beforeEach" block.
Example
db.create('user', { firstName: 'John', lastName: 'Smith' });
// db.add('user', { firstName: 'John', lastName: 'Smith' });
Alias
delete
, remove
Description Remove objects from a collection. Runs in a "beforeEach" block.
Example
db.remove('user', { firstName: 'John' });
// db.delete('user', { firstName: 'John' });
Description Drops the current database. Runs in a "beforeEach" block.
Example
db.dropDb();