Skip to content

Instantly share code, notes, and snippets.

@elliotf
Forked from warnero/gist:5404395
Last active December 16, 2015 08:19
Show Gist options
  • Save elliotf/5405164 to your computer and use it in GitHub Desktop.
Save elliotf/5405164 to your computer and use it in GitHub Desktop.
var dbURI = 'mongodb://localhost/noClear-example'
, mongoose = require('mongoose')
, clearDB = require('mocha-mongoose')(dbURI, { noClear: true })
, expect = require('chai').expect
;
var User = mongoose.model('User', new mongoose.Schema({
name: {type: String },
email: {type: String },
username: {type: String }
}));
var testUser = new User({
name: "Warner Onstine",
email: "[email protected]",
phone: "520-555-5555",
username: "warneronstine",
password: 'password test'
});
describe("Users", function(){
before(function(done) {
// this needs to be passed to the module at require time, up above
//options = {noClear: true};
if (mongoose.connection.db) return done();
mongoose.connect(dbURI, function(){
// use mocha-mongoose to clear the DB once at the start of the test
clearDB(done);
});
});
it("creates a new user", function(done){
testUser.save(function(err, doc){
if(err) return done(err);
expect(doc).to.exist;
expect(doc.name).to.equal("Warner Onstine");
done();
});
});
it("finds a user by email", function(done) {
testUser.save(function(err, doc){
if(err) return done(err);
expect(doc.email).to.equal("[email protected]");
var userQuery = User.findOne({'email': '[email protected]'});
userQuery.exec(function (err, user){
if(err) return done(err);
expect(user).to.exist;
expect(user.name).to.equal("Warner Onstine");
done();
});
});
});
it("finds a user by username", function(done) {
var userQuery = User.findOne({'username': 'warneronstine'});
userQuery.exec(function (err, user){
if(err) throw err;
expect(user).to.exist;
expect(user.name).to.equal("Warner Onstine");
done();
});
});
});
{
"dependencies": {
"mongoose": "~3.6.5"
},
"devDependencies": {
"mocha": "~1.9.0",
"chai": "~1.5.0",
"mocha-mongoose": "~0.1.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment