title: Unit/Integration testing Express REST API, mongoose, mocha, sinon, chai, supertest
body: <p>I'm quite confused about unit testing an Express REST API using mongoose.
  I've heard about supertest, sinon, chai and mocha.</p>
  
  <p>I want to write tests to :</p>
  
  <p>1) Test the API interface :</p>
  
  <p>Create a supertest server and make requests on it (GET /users, POST /users for example) and check that it calls the right routes.</p>
  
  <p>2) Test the mongoose queries :</p>
  
  <p>When a route is called, I want to check that the right mongoose function is called (find, findById) and that it returns the right data (fake data), something like :</p>
  
  <p>when User.findId is called, return fake user (data defined in beforeEach).
  Is it what sinon stubs are for ?</p>
  
  <p>3) Integration tests :
  Create a test database (created on test run), fill it and clean it when tests are finished.
  Make API calls (GET users, PUT users/1202082, DELETE users/1202082...) on a supertest server and check that the right data is read, created, updated, deleted...</p>
  
  <p>Is it the way to go ? 
  Simple examples for each case would be quite nice !</p>