mercredi 8 juillet 2015

Supertest returning 404, REST client gives 200

I have a route to get all friends in a database as follows:

router.get('/friends', function(req, res, next){
  Friend.find(function(err, friends){
    if(err){ console.log(err); }
    req.data = friends;
    next();
  })
});

Hitting this route with postman returns a list of all friends in the database successfully, and other queries from a web client behave normally. However, with Supertest when I try to test this route, a 404 is returned.

Here is the test:

var app = require('../server'); 
var should = require('should');
var supertest = require('supertest');
var request = supertest(app);


describe('Friends', function(){

  it('Should get all friends.', function(done){
    request
      .get('/friends')
      .expect(200)
      .end(function (err, res){
        res.status.should.equal(200);
        done();
      })
  });

});

Now, about 20% of the time, this test will execute successfully for no given reason. My question: Why does supertest return a 404, when all other methods to access the route return 200?

Aucun commentaire:

Enregistrer un commentaire