javascript - How can I clear my MongoDB collection between Mocha tests? Collection.remove({}) isn't working. . . -


how can clear mongodb collection between mocha tests? collection.remove({}) isn't working. . . .

does see doing wrong?

i've tried user.remove({}) syntax, well, no avail.

also, if have moment, seem reasonable way write test? i'm admittedly new testing.

const user = require('../../../app/models/user'); const config = require('../../../config/config'); const mongoose = require('mongoose'); const chai = require('chai'); const chaiaspromise = require('chai-as-promised'); const sinon = require('sinon'); const expect = chai.expect; chai.use(chaiaspromise); mongoose.connect(config.database, { usemongoclient: true }); mongoose.promise = global.promise;  describe('the user model', () => {   describe('during creation', () => {     beforeeach(() => {       // clear user collection of users.       user.remove({}, () => {});     });     aftereach(() => {       // clear user collection of users.       user.remove({}, () => {});     });      it('should store user in database', () => {       let userdata = {         email:    'blah@user.com',         password: '1234'       };       let user = new user(userdata);        return user.save().then(         newuser => {           return user.findone({_id: newuser._id}, (error,retrieveduser) => {             let expected = newuser._id;             let actual = retrieveduser._id;              console.log(actual,expected);             return expect(actual).to.equal(expected);           });         }       );     }); 

you want use deletemany.

depending on case, may want empty database beforeall , afterall specs rather beforeeach , aftereach spec.

also, ensure you're using testing database , not testing production or development database.

user.deletemany({}, (err) => console.log(err)); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -