title: How to add a delay before starting a Mocha test case?
body: <p>I'm writing a unit test for my simple Node.js application using Mocha. The application has a class which connects to a Mongo database, fetch the record, and store the formulated record as a field. Simply, the class looks like this:</p>
  
  <pre><code>SampleClass.prototype.record = []; // Store the loaded record
  SampleClass.prototype.init = function(db){
      var self = this;
      self.db = mongoose.connection; // Say we already have mongoose object initialized
      self.db.once('open',function(){
          /* schema &amp; model definitions go here */
          var DataModel = mongoose.model( /* foobar */);
          DataModel.findOne(function(err,record){
             /* error handling goes here */ 
  
             self.record = record; // Here we fetch &amp; store the data
          });
      });
  }
  </code></pre>
  
  <p>As seen from the snippet above, once the <strong>SampleClass.init()</strong> is called, the <strong>Sample.record</strong> will not instantly get populated from the database. The data is asynchronously populated once the event 'open' is fired. Thus, there will possibly be a delay after <strong>SampleClass.init()</strong> until the <strong>Sample.record</strong> is populated.</p>
  
  <p>So it comes into a complication when I write a Mocha test like this:</p>
  
  <pre><code>var testSampleClass = new SampleClass();
  
  describe('SampleClass init test',function(){
      testSampleClass.init('mydb');
      it('should have 1 record read from mydb',function(){
          assert.equal(testSampleClass.record.length,1);
      });
  });
  </code></pre>
  
  <p>The assertion above will always fail because <strong>testSampleClass.record</strong> will not get populated straightaway after <strong>init</strong>. It needs a gap of time to load the data.</p>
  
  <p>How can I delay the test case so it starts a few seconds or more after <strong>testSampleClass.init</strong> is called? Is it also possible to trigger the test case right after an event of my class is fired? Otherwise, this simple case will always fail which I know this is not correct at all.</p>