-
-
Save ralyodio/a8217751061ad6e738b9 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 cfg = require('../config') | |
, mongoose = require('mongoose') | |
, db = mongoose.connect(cfg.mongo.uri, cfg.mongo.db) | |
, User = require('../models/user') | |
, should = require('should') | |
, fakeUser; | |
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@ettinger.name'); | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment