Skip to content

Instantly share code, notes, and snippets.

@coiscir
Forked from ralyodio/gist:a8217751061ad6e738b9
Created December 16, 2012 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coiscir/77caf41f4ee62a7fedc0 to your computer and use it in GitHub Desktop.
Save coiscir/77caf41f4ee62a7fedc0 to your computer and use it in GitHub Desktop.
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();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment