1.2.0 • Published 8 years ago

mocha-mongoose v1.2.0

Weekly downloads
373
License
MIT
Repository
github
Last release
8 years ago

mocha-mongoose

Test helpers for using mongoose with mocha.

See the example spec (copied below) for more details.

Travis-CI Build Status Drone.io Build Status

Installation

  1. install via npm

    $ npm install mocha-mongoose

  2. require mocha-mongoose in your spec helper (easier) or in each spec file

    require('mocha-mongoose')('mongodb://your-mongodb-url-here');

  3. mocha-mongoose will automatically clear all of your collections before each spec run

  4. optionally provide a skip option to tell mocha-mongoose not to clear specific collections. require('mocha-mongoose')(dbURI, { skip: 'collectionname1', 'collectionname2' });

Example usage of automatically clearing the DB between specs:

This is a copy of example/test.js

var dbURI    = 'mongodb://localhost/demo-app-clearing-db'
  , should   = require('chai').should()
  , mongoose = require('mongoose')
  , Dummy    = mongoose.model('Dummy', new mongoose.Schema({a:Number}))
  , clearDB  = require('mocha-mongoose')(dbURI)
;

describe("Example spec for a model", function() {
  beforeEach(function(done) {
    if (mongoose.connection.db) return done();

    mongoose.connect(dbURI, done);
  });

  it("can be saved", function(done) {
    new Dummy({a: 1}).save(done);
  });

  it("can be listed", function(done) {
    new Dummy({a: 1}).save(function(err, model){
      if (err) return done(err);

      new Dummy({a: 2}).save(function(err, model){
        if (err) return done(err);

        Dummy.find({}, function(err, docs){
          if (err) return done(err);

          // without clearing the DB between specs, this would be 3
          docs.length.should.equal(2);
          done();
        });
      });
    });
  });

  it("can clear the DB on demand", function(done) {
    new Dummy({a: 5}).save(function(err, model){
      if (err) return done(err);

      clearDB(function(err){
        if (err) return done(err);

        Dummy.find({}, function(err, docs){
          if (err) return done(err);

          console.log(docs);

          docs.length.should.equal(0);
          done();
        });
      });
    });
  });
});

Example usage of manually clearing the DB:

This is a copy of example/manual.js

var dbURI    = 'mongodb://localhost/demo-app-clearing-db'
  , expect   = require('chai').expect
  , mongoose = require('mongoose')
  , Dummy    = mongoose.model('Dummy', new mongoose.Schema({a:Number}))
  , clearDB  = require('mocha-mongoose')(dbURI, {noClear: true})
;

describe("Example spec for a model", function() {
  before(function(done) {
    if (mongoose.connection.db) return done();

    mongoose.connect(dbURI, done);
  });

  before(function(done) {
    clearDB(done);
  });

  it("can be saved", function(done) {
    Dummy.create({a: 1}, done);
  });

  it("can save another", function(done) {
    Dummy.create({a: 2}, done);
  });

  it("can be listed", function(done) {
     Dummy.find({}, function(err, models){
      expect(err).to.not.exist;
      expect(models).to.have.length(2);

      done();
     });
  });

  it("can clear the DB on demand", function(done) {
    Dummy.count(function(err, count){
      expect(err).to.not.exist;
      expect(count).to.equal(2);

      clearDB(function(err){
        expect(err).to.not.exist;

        Dummy.find({}, function(err, docs){
          expect(err).to.not.exist;

          expect(docs.length).to.equal(0);
          done();
        });
      });
    });
  });
});
1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

12 years ago

0.0.4

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago