title: Unit testing mongoose model with Chai and Mocha
body: <p>I am trying to run the unit testing on my Nodejs application using Mocha and Chai. I am using Mongodb with Mongoose Framework. Here is my student.model.js </p>
  
  <pre><code>var mongoose = require('mongoose')
  var studentsSchema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      name: {
          type: String,
          required: true
      },
      email: String,
      phone: Number,
      address: String,
      username: String,
      password: String
  });
  
  
  var Students = mongoose.model('Student', studentsSchema);
  
  
  module.exports = Students;
  </code></pre>
  
  <p>And this is my student.model.test.js</p>
  
  <pre><code>let assert = require('chai').assert;
  var expect = require('chai').expect;
  var mongoose = require('mongoose');
  var Students = require('../models/students.model');
  
  describe ('Student',function(){
  
      before(function (done) {
          mongoose.connect('mongodb://localhost/mongoose_basics');
          const db = mongoose.connection;
          db.on('error', console.error.bind(console, 'connection error'));
          db.once('open', function() {
            console.log('We are connected to test database!');
            done();
          });
        });
  
      it('student works!',function(){
          var s = new Students({name:'krishna'});
  
          s.save(err =&gt; {
              if(err) { return done(); }
              throw new Error('Should generate error!');
            });
  
      });
      after(function(done){
          mongoose.connection.db.dropDatabase(function(){
            mongoose.connection.close(done);
          });
        });
  
  });
  </code></pre>
  
  <p>I want to run the unit test using Mocha and Chai. When I run <code>mocha student.model.test.js</code> I am getting following error </p>
  
  <pre><code>TypeError: Students is not a constructor
  </code></pre>
  
  <p>I am not sure why it's returning not a constructor. </p>