-
-
Save elliotf/5405164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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