title: Testing mongodb database with mocha and request
body: <p>I've got 2 questions that I am searching for an answer for 3 days so far and can't figure it out.<br>
  1. When I should connect to the database when testing?<br>
  2. I always get an error when running the test: { "before each" hook for "should list all books on /book GET" } and haven't yet found a solution or the exact cause for it. What am I doing wrong? The only answer I have so far is not to call done() twice in beforeEach() but I ain't doing it...</p>
  
  <pre><code>var chai      = require('chai'),
      expect    = chai.expect,
      request   = require('request'), 
      mongoose  = require('mongoose'),
      Book      = require('./../models/book');
  // book = require('../model')
  
  mongoose.createConnection('mongodb://localhost/books');
  
  describe('Testing the routes', () =&gt; {
      beforeEach((done) =&gt; {
          Book.remove({}, (err) =&gt; {
              if (err) {
                  console.log(err);
              }
          });
          var newBook = new Book();
          newBook.title  = "Lord Of The Rings";
          newBook.author = "J. R. R. Tolkien";
          newBook.pages  = 1234;
          newBook.year   = 2000;
          newBook.save((err) =&gt; {
              if (err) {
                  console.log(err);
              }
              done();
          });
      });
  
      it('should list all books on /book GET', (done) =&gt; {
          var url = 'http://localhost:8080/book';
          request.get(url, (error, response, body) =&gt; {
              expect(body).to.be.an('array');
              expect(body.length).to.equal(1);
              done();
          });
      });
  });
  </code></pre>