secret
Last active — forked from chovy/gist:a8217751061ad6e738b9

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
var cfg = { mongo: { uri: 'localhost', db: 'so-q-13898783' } } // require('../config')
, mongoose = require('mongoose')
, db = mongoose.connect(cfg.mongo.uri, cfg.mongo.db)
//, User = require('../models/user')
, should = require('should')
, fakeUser;
 
var User = db.model('users', mongoose.Schema({
username: { type: String, required: true },
email: String,
password: String
}));
 
mongoose.connection.on('error', function(err){
console.log(err);
});
 
//console.log('NODE_ENV: '+process.env.NODE_ENV);
 
//if ( process.env.NODE_ENV !== 'test' ) {
// console.log("Woops, you want NODE_ENV=test before you try this again!");
// process.exit(1);
//}
 
after(function(done){
db.connection.db.dropDatabase(function(){
db.connection.close(function(){
done();
});
});
});
 
describe('User', function(){
beforeEach(function(done){
//clear out db
User.remove(function(){
fakeUser = {
username : 'Test1'
, email : 'test1@example.com'
, password : 'asdf123'
};
 
done();
});
});
 
after(function(done){
//clear out db
User.remove(function(){
done();
});
});
 
describe('#save()', function(){
var user;
// you can use beforeEach in each nested describe
beforeEach(function(done){
user = new User(fakeUser);
done();
});
 
// you are testing for errors when checking for properties
// no need for explicit save test
it('should have username property', function(done){
user.save(function(err, user){
// dont do this: if (err) throw err; - use a test
should.not.exist(err);
user.should.have.property('username', 'Test1');
user.should.have.property('email', 'test1@example.com');
//user.password.should.not.equal(fakeUser.password);
done();
});
});
 
// now try a negative test
it('should not save if username is not present', function(done){
user.username = '';
user.save(function(err, user){
should.exist(err);
should.not.exist(user);
done();
});
});
});
 
describe('#find()', function(){
var user;
// you can use beforeEach in each nested describe
beforeEach(function(done){
user = new User(fakeUser);
user.save(function(err, user){
done();
});
});
 
it('should find user by email', function(done){
User.findOne({email: fakeUser.email}, function(err, user){
// dont do this: if (err) throw err; - use a test
should.not.exist(err);
should.exist(user);
user.should.have.property('email', 'test1@example.com');
done();
});
});
 
it('should find user by username', function(done){
User.findOne({username: fakeUser.username}, function(err, user){
// dont do this: if (err) throw err; - use a test
should.not.exist(err);
should.exist(user);
user.should.have.property('email', 'test1@example.com');
done();
});
});
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.