title: Mocking database in node.js?
body: <p>How would I mock out the database in my node.js application, which in this case uses <code>mongodb</code> as the backend for a blog REST API ?</p>
  
  <p>Sure, I could set the database to a specific <code>testing</code> -database, but I would still save data and not test my code only, but also the database, so I am actually not doing unit testing but integration testing.<br>
   So what should one do? Create database wrappers as a middle layer between application and db and replace the DAL when in testing?</p>
  
  <pre><code>// app.js  
  var express = require('express');
      app = express(),
      mongo = require('mongoskin'),
      db = mongo.db('localhost:27017/test?auto_reconnect');
  
  app.get('/posts/:slug', function(req, res){
      db.collection('posts').findOne({slug: req.params.slug}, function (err, post) {
          res.send(JSON.stringify(post), 200);
      });
  });
  
  app.listen(3000);
  </code></pre>
  
  <hr>
  
  <pre><code>// test.js
  r = require('requestah')(3000);
  describe("Does some testing", function() {
  
    it("Fetches a blogpost by slug", function(done) {
      r.get("/posts/aslug", function(res) {
        expect(res.statusCode).to.equal(200);
        expect(JSON.parse(res.body)["title"]).to.not.equal(null);
        return done();
      });
  
    });
  ));
  </code></pre>