title: NodeJs, Mocha and Mongoose
body: <p>I have the follow structure:</p>
  
  <pre>
  |server
   |db
    |mongooseTest.js
   |test
    |userModel.test.js
   |user
    |userModel.js
  </pre>
  
  <p>With their code:</p>
  
  <p><em>mongooseTest.js</em></p>
  
  <pre>
  
      var mongoose = require('mongoose');
  
      module.exports = function() {
          var db = mongoose.createConnection('localhost', 'dbUnitTest');
  
          db.on('connected', function() {
              console.log('DB: ' + db.name + ' local: ' + db.host + ':' + db.port);
          });
  
          db.on('error', function(err) {
              console.log(err);
          });
  
          return db;
      };
  
  </pre>
  
  <p><em>userModel.test.js</em></p>
  
  <pre>
  
      var assert = require('assert'),
          should = require('should'),
          conn = require('../db/mongooseTest'),
          UserModel = require('../user/userModel');
  
      describe('User Model', function() {
          describe('Save', function() {
  
              it('Saving...', function() {
                  var db = conn();
                  var Model = db.model('User');
                  var userModel = new Model({
                      name: 'My Name',
                      email: 'contact@com.br',
                      pass: 'anything123'
                  });
  
                  userModel.on('save', function(user) {
                      console.log('Passed by save event handle from user');
                  });
  
                  userModel.save(function(err, user) {
                      console.log('Passed by save from user');
                      if(err) console.log(err);
                      console.log(user);
                  });
              });
          })
      })
  
  </pre>
  
  <p><em>userModel.js</em></p>
  
  <pre>
  
      var mongoose = require('mongoose'),
          crypto = require('crypto'),
          Schema = mongoose.Schema;
  
      var setPass = function(value) {
              var salt = 'anyRandomSaltValue';
              this.set('salt', salt);
              var pass = hashPass(value + salt);
              return pass;
          }
  
      var hashPass = function(value) {
              return crypto.createHash('sha1').update(value).digest('HEX');
          }
  
      var userSchema = new Schema({
          name: {
              type: String,
              required: true
          },
          email: {
              type: String,
              required: true,
              unique: true
          },
          pass: {
              type: String,
              required: true,
              set: setPass
          },
          salt: {
              type: String,
              required: true
          }
      });
  
      userSchema.method({
          validatePass: function(senha) {
              var salt = this.get('salt');
              var passSha = hashPass(senha + salt);
              return passSha === this.get('senha');
          },
      });
  
      userSchema.static({
          findByEmail: function(email, success, error) {
              this.findOne({
                  email: email
              }, function(e, o) {
                  if(e) {
                      if(error) error(e);
                  } else {
                      if(success) success(o);
                  }
              });
          },
      });
  
      module.exports = mongoose.model("User", userSchema);
  
  </pre>
  
  <p>The problem is...
  When I run "mocha" to execute my unit tests, the callback of the save function  is not performed.</p>
  
  <p>Thanks for all!</p>